Day 5 — Conditions and branching
A flow that always does the same thing is a macro. A flow that decides is an automation.
The Condition action
{
"trigger": { "type": "manual", "body": { "Name": "Anita", "Amount": 52000 } },
"actions": [
{
"name": "Is it a big order?",
"type": "Condition",
"expression": "@greater(triggerBody()?['Amount'], 25000)",
"yes": [
{ "name": "Flag for approval", "type": "Compose",
"input": "@{triggerBody()?['Name']} needs manager approval" }
],
"no": [
{ "name": "Auto approve", "type": "Compose",
"input": "@{triggerBody()?['Name']} approved automatically" }
]
}
]
}
The run history shows which branch it took, and marks the other one Skipped. Change the Amount to 5000 and run it again.
The comparison functions
| Function | Means |
|---|---|
equals(a, b) | a is the same as b |
greater(a, b) | a is more than b |
greaterOrEquals(a, b) | a is at least b |
less(a, b) | a is less than b |
lessOrEquals(a, b) | a is at most b |
not(x) | the opposite |
contains(a, b) | a contains b - works on text and on arrays |
empty(x) | null, blank text, empty list or empty object |
@equals(triggerBody()?['Region'], 'North') @equals(triggerBody()?['Region'], 'north') @greater(triggerBody()?['Amount'], 25000) @contains(triggerBody()?['Tags'], 'vip') @empty(triggerBody()?['Phone']) @not(empty(triggerBody()?['Region']))
equals(toLower(a), toLower(b)). Do it by habit and one whole class of bug disappears.and, or, and why they look strange
There is no &&. You wrap the whole thing in and() or or(), and every test goes inside as an argument.
@and(equals(triggerBody()?['Region'],'North'), greater(triggerBody()?['Amount'],25000)) @or(equals(triggerBody()?['Course'],'Excel'), equals(triggerBody()?['Course'],'SQL')) @and(greater(triggerBody()?['Amount'],1000), less(triggerBody()?['Amount'],100000)) @or(and(equals(triggerBody()?['Region'],'North'), greater(triggerBody()?['Amount'],50000)), equals(triggerBody()?['Course'],'SQL'))
That last one is "North and over 50,000, or any SQL enrolment". Nesting and inside or is how you build any rule you like - it just reads badly, so keep them short.
A real routing flow
{
"trigger": { "type": "manual",
"body": { "Name": "Vikram", "Course": "Power BI", "Amount": 14000, "Region": "West" } },
"actions": [
{ "name": "Init route", "type": "InitializeVariable", "variable": "route", "value": "" },
{
"name": "Big or small",
"type": "Condition",
"expression": "@greaterOrEquals(triggerBody()?['Amount'], 25000)",
"yes": [
{ "name": "Needs approval", "type": "SetVariable", "variable": "route", "value": "Manager approval" }
],
"no": [
{
"name": "Priority region?",
"type": "Condition",
"expression": "@or(equals(toLower(triggerBody()?['Region']),'north'), equals(toLower(triggerBody()?['Region']),'west'))",
"yes": [
{ "name": "Fast track", "type": "SetVariable", "variable": "route", "value": "Fast track" }
],
"no": [
{ "name": "Standard", "type": "SetVariable", "variable": "route", "value": "Standard queue" }
]
}
]
},
{ "name": "Tell the team", "type": "SendEmail",
"to": "team@dai-academy.com",
"subject": "New enrolment: @{triggerBody()?['Name']}",
"body": "@{triggerBody()?['Name']} - @{triggerBody()?['Course']} - routed to @{variables('route')}" }
]
}
Run it, then change the Region to "East" and run again. Watch the route change to Standard queue.
Switch - one value, many outcomes
{
"trigger": { "type": "manual", "body": { "Course": "SQL" } },
"actions": [
{
"name": "Which trainer",
"type": "Switch",
"on": "@triggerBody()?['Course']",
"cases": {
"Advanced Excel": [ { "name": "Excel trainer", "type": "Compose", "input": "Assigned to Anita" } ],
"SQL": [ { "name": "SQL trainer", "type": "Compose", "input": "Assigned to Rahul" } ],
"Power BI": [ { "name": "BI trainer", "type": "Compose", "input": "Assigned to Meera" } ]
},
"def": [ { "name": "Unassigned", "type": "Compose", "input": "No trainer for this course yet" } ]
}
]
}
Change the Course to "Tableau" and it falls through to the default. Switch matches exactly - "sql" in lower case will not match "SQL", so normalise the value before you switch on it.
if() - a decision inside one expression
When both branches are one small value, a full Condition is heavy. if() does it inline.
@if(greater(triggerBody()?['Amount'], 25000), 'Large', 'Small')
@if(empty(triggerBody()?['Phone']), 'No phone', triggerBody()?['Phone'])
Dear @{if(empty(triggerBody()?['Name']), 'customer', triggerBody()?['Name'])}
@if(greater(triggerBody()?['Amount'],100000),'A',if(greater(triggerBody()?['Amount'],25000),'B','C'))
Try these yourself
- Write a condition that is true only when the amount is between 10,000 and 50,000.
- Make a case-insensitive comparison of a region name.
- Build a Switch on course name with four cases and a default.
- Rewrite a two-branch Condition as a single if() inside a Compose.
- Explain why nesting Conditions three deep is a bad idea.
