Flow tips: conditional triggers and data gateway issue

Some months ago, Serge Luca came up with a great tip to add conditional logic to your flow definitions so that a flow would ‘skip’ under certain conditions. However, there is a catch if you are using Flow to access on-premise SharePoint data through a data gateway.

As there are only a finite number of flows that can run per user license, we do not want to run flows unnecessarily.

For example, if we create a blank flow with the trigger ‘When a file is created or modified’ it will run for every document modified in a library. Typically we would add a condition and check the content type inside the flow if we only want it to run for a specific content type.

Microsoft Flow screenshot of Condition fields to fill in

It works perfectly but also consumes a flow each time it runs, even if in most cases the document being modified is not a ‘Procedure’ (as in my example above).

The workaround, which still works as of first quarter 2019, is to export the flow as a .zip package, unzip to your PC and edit the JSON to add a trigger condition.

Microsoft Flow screenshot - More drop down options to Export

Unpack the zip file.

Screenshot of Microsoft Flow folders

Click down through the ‘Microsoft Flow’ folders, through three levels, until the ‘definition.json’ is available. Open it in a suitable JSON editor such as Visual Code.

Screenshot of Microsoft Flow folders

Find the ‘triggers’ section of the JSON and add your ‘conditions’.

Screenshot of json code to enter for conditions


},
"conditions": [
{
"expression": "@and(equals(triggerBody()?['{ContentType}']?['Name'], 'Procedure'),equals(triggerBody()?['ProcedureID'], null))"
}
]

In this example, I am checking two conditions: ‘Content type = Procedure’ and the custom column of ProcedureID is empty.

This works for fields other than content type. You can adjust the trigger condition as required for your specific purpose, changing the trigger test to any other property available to you.

Repack your zip file at the top level, ready to re-import back into Flow. The name of the zip file is not important.

When the package imports, you may need to update some settings, click the links to do so.

Screenshot of Microsoft Flow - Import and review package content pages

Run your flow; nothing appears in your Run History. Click on the ‘See all’ link and select the ‘Checks (no new data)’ view.

Screenshot of Microsoft Flow - Run history

As you can see the flow now skips and does not consume a flow resource.

So far, so good…

However, I recently configured a flow to update a SharePoint document library through a data gateway and noticed that neither the content type name or the content type ID are available inside a flow through a gateway.

Screenshot of Microsoft Flow - data gateway and dynamic content options

Using an extra ‘Send an HTTP Request’ action you can access the content type inside the flow and can add it to your flow condition. However, this does not help you when testing the condition in the trigger via the JSON modification above.

Microsoft may make changes to the data gateway to make the content type available. Until then our only option was to create another custom column in the ‘Procedure’ content type and use this new flag as the trigger condition.

,
"conditions": [
{
"expression": "@and(equals(triggerBody()?['ProcedureFlag'], 'YES'),
equals(triggerBody()?['ProcedureID'], null))"
}
]

I hope this post saves some of the time it took trying to work around this odd data gateway problem.

Do you have any more queries about Microsoft Flow? Our consultants are based in Brisbane and ready to help.