Triggers play an essential role in Power Automate since we could not even start the Flows without them. Triggers can have information that could be useful in the Flow, like the details of the item that was created in Forms or the message that was published in Teams, for example. But how do we access the values of the trigger in the Flow? You can select them in the “Dynamic content” tab, but what if you need them in a formula, for example? Here’s where the “triggerBody” function comes into play.
Let’s explore how to use it.
Usage
It follows a simple pattern, and no parameters are required:
triggerBody()
It will return the list of items provided when the action was triggered. The return is in JSON format. In case you’re not familiar with JSON, no worries; I have a general overview of what you should know here.
Here’s a simple trigger and “compose” action to show what was provided.
Let’s provide some data and run it.
Here’s the result.
We’ll get the following:
{
"text": "Manuel Gomes",
"boolean": false,
"number": 41,
"email": "manuel@manueltgomes.com"
}
As mentioned before, we’ll get a JSON file. Notice also that the values you request to use (Name, Age, etc) are not the ones that are referenced in the trigger. There’s no official mention from Microsoft on why this was done like this, but I would guess it was because the fields may contain special characters, and it would make the JSON complex or fail depending on the special character.
How to access the information?
Since it’s a JSON, we can access it by doing the following:
- Call the function
- Indicate the field that we want to get the data using the square brackets and key.
For example, if we want to get the data, we’ll do the following:
triggerBody()['text']
We’ll get:
How do I know the keys to get the data?
If you add multiple options in the trigger and run the Flow, you’ll see Microsoft’s convention. The key is the type of the field plus a number in case there are multiple with the same name.
[type]_[number]
For example:
triggerBody()['text']
triggerBody()['number_1']
To be sure that you’re using the correct keys, you can follow the next section’s steps and pick them from the “dynamic content” or have a compose action at the start of the Flow, run it once and see what the values that you can get from the action are, like this.
The result will provide you with the certainty of the value to call.
You’re using it without knowing
Please note that, if you select the items from the “Dynamic content” Power Automate will use this function for you. Here’s an example.
As you can see, when we hover the mouse, the value that you selected is the same as above, or:
triggerBody()['text']
Limitations
Depending on the size of your string, your expression may return an error, even if it’s correct. Please note that the expressions have a max size of 8,192 characters. If you have an expression that is even bigger than 1000, I would strongly advise that you break it into smaller, manageable formulas.
Since we don’t have any parameters in this function, you’ll only run into this limitation in case you’re integrating the “triggerBody” function in other expressions.
Recommendations:
Here are some things to keep in mind.
Use “debug” compose actions
As I mentioned above, I would keep a “compose” action at the beginning of the Flow to ensure that I have the values returned at the start. This way, if something goes wrong with the Flow, I can check whether the values I got from the beginning are correct. If not, it will remove a lot of uncertainty since I know that the error is either inside the Flow or with the values returned.
Pick from the list
As I mentioned before, you’re probably always picking the values from the “dynamic content” list to ensure you’re always using the correct value. Please note that you can do this even if you’re writing an expression like this:
It’s the same result and will remove many mistakes and issues.
Sources:
Microsoft’s triggerBody Function Reference
Back to the Power Automate Function Reference.
Photo by MIOPS Trigger on Unsplash