Power Automate – Get the Flow Trigger Context Information
Business developers, do you use Power Automate to automate your organization’s business processes? And are you developing flows using the Common Data Service connector (Current Environment)? If so, there have probably been times when you needed information about the context in which the flow was triggered. Here’s how to obtain this info.
When you generate flows in the Power Automate environment through the Dataverse (Current environment) connector, some informations are very useful. Indeed, if you can access the data of the trigger context of a flow, you’ll be able to reuse it in the business logic of your flow.
Here are some examples where this could be useful:
- You need to find out the Dataverse environment user who is executing the flow;
- You need to know what action triggered the flow to apply conditional logic (e.g., create, update, delete).
In this article, we will discuss three methods for:
- Extracting the trigger context;
- Using the information in the business logic of a flow.
First, let’s take a look at the trigger output.
Let’s review the logs of a flow that triggers on an event of the Common Data Service (current environment) connector.
By opening the flow run you can examine the result of the first action, the trigger.
Notice the last properties of the output body (the ones that start with an uppercase letter). These properties are always found in the output of the trigger, no matter what table and what message triggers the flow. However, in our example, these properties are not available in dynamic content and they don’t appear like other properties in the account record.
In order to be able to use these values, we’ll need to find a way to extract them. But how?
Extracting a flow trigger context with 3 simple methods
Let’s go over three easy methods that will allow you to extract and consume context values.
1. Direct query to the triggerBody()
In the Expression, type in this line of code:
You can thus extract a single property. In the case illustrated above, it is RunAsSystemUserId. It’s a quick way to obtain the values, however, each value must be extracted one by one.
2. JSON parse the triggerBody()
This method is particularly interesting. It can be implemented in just one step, but even better, it allows you to use all the extracted properties directly in the Dynamic Content tab. Keep in mind that flows are billed by number of actions, so every attempt to reduce the number of actions can be beneficial to you.
So, let’s give it a go.
First, we need to configure a “Parse JSON” step in the flow.
Select the triggerBody() output for content and then click on “Generate from sample” to insert a JSON sample. (Generate from sample).
To get the sample, you will have to make the flow trigger in order to inspect the trigger body from an execution log.
Then, copy the JSON output and remove all the properties that are not necessary, so that only the properties of interest are kept (in our case, SdkMessage, RunAsSystemUserId).
Finally, paste the simplified JSON and this will automatically generate the JSON schema.
The values will then be available directly in the Dynamic Content tab, ready for use. It’s that simple!
3. Create a Child Flow
With this method, you can encapsulate the JSON parsing logic that was used in the second method in a Child Flow that can then be reused in any other flow in your environment.
This is an excellent option because it allows you to reuse functionalities across the organization. And most of all, because it makes the programming accessible to people who are not necessarily familiar with the intricacies of JSON parsing and property digging.
We will not deep dive into Child Flow development (let’s keep that for another post). But in the meantime, here are the main steps:
Create a new flow (inside a solution)
1. Manual trigger
Define an input parameter called TriggerBody.
(Exact same step seen in the second method; Parse JSON.)
3. Respond to PowerApps
First, you must define the output parameters to map the two outputs of the Parse JSON step. Once this is done, anyone can easily drop the Child Flow into their own flow. That’s what’s great about it: code once, reuse anywhere!
- Add a Child Flow step.
- Choose your newly created Child Flow from the dropdown menu.
- In the TriggerBody input, enter triggerBody() in the Expression tab.
Once again, the trigger context values will be directly available in the Dynamic Content tab, ready to be used again.
Here’s an example of the Child Flow output when executed. That’s all there is to it!
And how is this useful, concretely?
Now that you know how to extract trigger context values, you can use them inside the logic of any flow. For example, you could use the extracted trigger context values to complete an email that is sent whenever an action occurs on an account (see below).
In the future, when you develop flows using the Common Data Service Connector (current environment), the context for executing flows will no longer be a secret.
Want to find out more?
Our team can support you with personalized advice, training or new solutions tailored to your needs. Contact one of our experts to optimize the use of your technologies and your daily processes.
This article is an initiative of our expert David Rivard.