Alter control flow using a control action
Control actions help you add decisions and alternate action paths to your workflow. The news monitoring app uses a control action to check an article's sentiment ranking and branch the workflow path, based on whether the ranking is positive or negative. In this unit, we look at the four control actions that help you manage the execution path through your workflow.
Condition action
The Condition action is actually an If
statement that lets you split your workflow into two paths, based on whether the specified condition evaluates to true or false. The condition has three parts: a Boolean expression and two actions. At runtime, the Azure Logic Apps execution engine evaluates the expression, and chooses an action based on whether the expression is true or false.
For example, you might want to route an expense report to a different manager based on the amount. If you're processing an email, you might need to test whether the message is flagged as high-priority. In the example news monitoring app, use the Condition action to branch based on the article's sentiment label. The following diagram shows how the workflow uses the Condition action:
Types and operators
In your Condition action, you can compare numeric, string, Boolean, and JSON objects. The following pseudocode shows an example for several data types:
if (Favorited is-equal-to true) ... // Boolean type
if (URL does-not-contain "mycompany") ... // String type
if (confidenceScores is-equal-to 1) ... // Numeric type
Each data type provides a set of operators to use in your comparisons. The following table lists the operators for each type:
Numeric | String | Boolean | JSON objects |
---|---|---|---|
is-equal-to | is-equal-to | is-equal-to | is-equal-to |
is-not-equal-to | is-not-equal-to | is-not-equal-to | is-not-equal-to |
is-greater-than | contains | ||
is-greater-than-or-equal-to | does-not-contain | ||
is-less-than | starts-with | ||
is-less-than-or-equal-to | does-not-start-with | ||
ends-with | |||
does-not-end-with |
Most of the operators are intuitive, but two have the following considerations:
String comparisons are all case sensitive.
JSON comparisons use a deep equals. This comparison means that the equality operators compare the entire object, including any descendant tokens inside complex objects.
Combine expressions using AND and OR
Azure Logic Apps lets you create complex expressions by combining conditions using the AND or OR operator.
For example, suppose you wanted to identify articles that are neutral and mixed in their sentiment. You can write an expression similar to the following pseudocode:
if (sentiment is-equal-to neutral OR sentiment is-equal-to mixed)
Data availability
Azure Logic Apps makes the outputs from all previous steps in a workflow available in subsequent steps. This feature means that your expressions can use values generated by any previous step. You can even combine values from different steps into one expression.
For example, the following pseudocode looks for articles with positive sentiment that your company write. The sentiment value comes from the Sentiment action, while the URL value comes from the On a new news article trigger.
if (sentiment is-equal-to positive AND URL does-not-contain "mycompany")
Create complex expressions with groups
Suppose you want to build an expression that identifies influential and relevant articles. You decide that the following criteria help qualify these articles:
- The sentiment is positive.
- The category is "Sports" or "Business".
To capture these criteria, you want to build an expression similar to the following pseudocode, but you can't use the AND with the OR operator:
if (sentiment is-equal-to positive AND Category is-equal-to Sports OR Category is-equal-to Business) // Error, can't mix AND with OR
Azure Logic Apps indirectly supports this expression, so you can still complete this task, but in another way. Instead, you can use a group, which is a sequence of expressions combined with either AND or OR. You can't mix AND and OR within a group.
Let's look at a few examples expressed as pseudocode. To make the examples easier to read, use lowercase letters to represent the expressions that you need to combine. The first example is a valid group, but the second one isn't valid:
if (a AND b AND c) // OK
if (a AND b OR c) // Error, can't mix AND and OR
Instead, for the preceding example, you have to create a group for the expressions connected by AND. You then use OR to connect the group to the other expression. To represent a group, use brackets in the following pseudocode:
if ([a AND b] OR c) // OK
Switch action
The Switch action compares a value against several cases and executes only the one that matches. A Switch expression supports the string and integer data types. The Switch action also includes a default action that runs if no match exists.
For example, suppose you're processing an email message and want to respond differently based on the message's importance. In an Outlook email, the Importance value is an integer between 0 and 2. To build a switch action for email importance, you might use logic similar to the following pseudocode:
switch (Importance)
case 0: ... // Actions for high importance
case 1: ... // Actions for medium importance
case 2: ... // Actions for low importance
default: ...
For each loop
A For each loop action processes an array. The loop performs the same actions on each array item. By default, the actions for each array item run in parallel, although you can control this behavior in the loop's settings.
For example, the Bing Search connector has a List news by query action. The action's output is an array of news articles, which contains a URL for each article. Suppose you wanted to insert each URL into a row in a database. You can use the For each action to do the processing as the following pseudocode shows:
foreach url in NewsArticle
insert-row
Until loop
The Until loop action runs a group of actions at least once and repeats until any of the following stop criteria are true:
- Condition: An expression that is evaluated after every iteration
- Count: The maximum number of iterations. The default is 60.
- Timeout: The maximum allowed run duration, which is specified in ISO 8601 format. The default is one hour.
You can use this loop to process data, but this loop is also a good option for cases such as retrying a networking operation until the operation succeeds or times out. For example, suppose your workflow has to run an action that makes an HTTP request. You can use an Until loop in the example described by the following pseudocode where the PT5M
time value is equal to five minutes:
repeat
HTTP GET
until (StatusCode is-equal-to 200 OR Count is-greater-than 3 OR Timeout is-greater-than PT5M)
Control actions in the designer
Control actions run natively within the Azure Logic Apps execution engine, so they're called "built-in" or "in-app" operations. In the connector gallery, all control actions are packaged inside the Control collection. After you find this collection in the gallery, you can select any of the previously described control actions. The following screenshot shows the control actions available for you to select:
Tip
If you don't see all the actions, next to collection name, select See more.
To evaluate the sentiment in the workflow, add the Condition control action, which adds an If
statement. The Condition action provides a way to build your Boolean expression. To create complex expressions using groups, the New item list includes the Add group option, for example:
The next unit shows how to add and configure a control action with two branches in the workflow designer.