Featured image of post Environment Variables in Microsoft Power Automate Revisited

Environment Variables in Microsoft Power Automate Revisited

I had an enjoyable time at the D365UG UK Cambridge event earlier this week. During this, we saw some great sessions from the likes of Ana Demeny, Andrew Bibby and Feridun Kadir, covering a variety of useful Dynamics 365 and Power Platform related topics. I was also fortunate to present a session myself, all about Environment Variables. The talk linked closely to a blog post I did a couple of weeks back, and I had an excellent question come up regarding the approach I used to retrieve the Environment Variable value within a Power Automate flow. If you recall from the post, I was using a somewhat convoluted method to retrieve the actual Environment Variable value I wanted via multiple List record and Apply to each steps, which I’ve illustrated in the screenshot below:

First, let’s highlight why going down this approach may be less than ideal:

  • An Apply to each step does introduce a performance overhead into your flows. Your mileage may vary, but in the case of the above flow, there was an additional second’s worth of execution time introduce into the equation. For much larger flows, this approach could extend this delay even further.
  • The above Flow does not make it immediately clear that all we are interested in retrieving the first value from our list records step. You would have to add on a specific comment to make this apparent.

Thankfully, there is a better way  - and for new readers or for those who have jumped across from the previous post (hello, by the way 🙂 ), we’ll now show you how to do this.

To begin with, we want to expand out our List ____ Environment Variable Current Value action step and press the cross icon over the Environment Variable Definition field that’s referenced in there:

With that removed, we can now drag this action step and place it directly after the existing List ____ Environment Variable action:

Now, we can look to replace the dynamic content field with a custom formula to return the attribute of the first record from the previous step. The syntax to use for this is as follows:

body()?[‘value’]?[0]?.environmentvariabledefinitionid

So if we’ve called our List ____ Environment Variable action step List MyVariable Environment Variable, the formula would need to look like this:

body(‘List_MyVariable_Environment_Variable’)?[‘value’]?[0]?.environmentvariabledefinitionid

Don’t forget the single quotes! If done correctly, your step should now resemble the below:

Next, we need to perform the same steps for our Set Variable action step; namely, remove the dynamic content, drag it so that the action succeeds from our List ____ Environment Variable Current Value and add in a new formula. This should do the same as above, but referencing the field we require from the Environment Variable Value table:

body()?[‘value’]?[0]?.value

e.g.

body(‘List_MyVariable_Environment_Variable_Current_Value’)?[‘value’]?[0]?.value

It should look something like this if done correctly:

With all that done and dusted, you can then remove the redundant Apply to each steps from the flow and save it, to confirm that all changes are applied successfully.

All in all, with this alternative route, we’ve been able to reduce the number of steps in the flow, simplifying its structure in the process, and improve it’s performance too; the 1-second delay spoken about earlier vanishes entirely as a consequence of making these changes. Many thanks to the Cambridge Dynamics 365 community for raising this as a question, and I hope this post provides enough detail on how to adapt it yourself within your environment. 😃

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy