Power Automate Primer
Overview
My hope is that once you're finished going through this document, you will have no fear in reading Power Automate expressions, be able to interpret what every single bracket means, and get started writing your own.
Things you will need
The language is called the Azure Workflow Definition Language (WDL). It is used in both Azure Logic Apps and Power Automate.
Make this guide your best friend when you're making Flows using Power Automate.
Values
Strings
Strings are indicated by single quote marks (').
'this'
is a string.
"this"
is not a string - Power Automate will throw an error.
Logical
true
, false
, and null
are written like this. These need to be written as an expression, not just as a string.
Numbers
Numbers like 4
are written by themselves, no quote marks.
Numbers will not be automatically converted to string or vice versa. Make sure to use a conversion function before trying to e.g. compare a string and a number together.
Functions
There are no operators
You can't do things like 1 + 2
in WDL.
You can only do add(1, 2)
.
This applies for all of arithmetic (sub()
, mul()
, div()
, mod()
, etc.) and logic (equals()
, not()
, greater()
, and()
, or()
, etc.). Make sure you consult the function reference for all of the available functions that you thought was an operator.
Blocks
This is how to work with "blocks" within Power Automate.
Block reference
When referring to a block, refer to it as a string.
'Compose'
If the name of the block has a space in it, replace that space with an underscore.
'Get_file`
Outputs
To get to the entire output of a block, use the outputs()
function.
outputs('Get_file')
In many cases, the output will have a body
property. For example, an output might look like this:
{
"statusCode": 200,
"headers": {
...
},
"body": {
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#directoryObjects",
"value": [
{
...
},
{
...
},
{
...
},
...
]
}
}
To shortcut to the body
property of the output, use the body()
function.
body('List_group_members')
Expressions
When you copy tags from a block, if you see a @{}
, delete it. It is not part of the expression.
Objects and arrays
Compose
If you write a a JSON object or array into Compose, Power Automate will interpret that as an object or array respectively.
Arrays
Creating arrays
Let Muppets
be a Compose block with the following input:
["Bert", "Ernie", "Elmo", "Grover"]
Alternatively, the input might be an expression (note the single quote marks):
createArray('Bert', 'Ernie', 'Elmo', 'Grover')
Element access
To access the first element of an array, use [0]
. WDL uses zero-based indexing (the first element is 0, the second is 1, the third is 2, etc.)
outputs('Muppets')[0]
// Bert
Objects
Let 'Muppets' be a Compose block with the following input:
[
{
"name": "Bert",
"colour": "yellow"
},
{
"name": "Ernie",
"colour": "orange"
},
{
"name": "Elmo",
"colour": "red"
},
{
"name": "Grover",
"colour": "blue"
},
{
"name": "Big Bird"
}
]
Property access
To get to Bert's colour, you can use either "dot syntax" or "square bracket syntax".
outputs('Muppets')[0].colour
// yellow
outputs('Muppets')[0]['colour']
// yellow
Optional property access
Sometimes, you could be looked for a property on an object that might not exist. In this case, use a ?
operator.
outputs('Muppets')[3]?.colour
// yellow
outputs('Muppets')[3]?['colour']
// yellow
outputs('Muppets')[4]?.colour
// null
Rules of thumb
Avoid too many nested loops
Using existing functions are a much more efficient way of doing things compared to doing nested loops.
Refactor out common functions into their own Flows
Use the HTTP connector to make your own "functions". Example: have a flow that decorates HTML text with your company's letterhead. Whenever you have to send outgoing emails, pass the email body through your "Decorate Email" Flow, which returns a decorated email for you to send.
Tips
Distinct array
union()
an array with itself to create a distinct array.
union(outputs('Muppets'), outputs('Muppets'))