Get up to Speed Quickly on Power Automate: ‘Variables’ Actions (Blog 5 of 5)

In the first four blog posts in this series, we covered a lot of material. If you haven’t already read the previous posts, you can read them here:

Blog Day 1 – Big Picture Power Automate Concepts for the D365 System Admin
Blog Day 2 – Power Automate Triggers for the Dynamics System Admin
Blog Day 3 – Power Automate List Rows
Blog Day 4 – Apply to Each actions for the System Admin

These posts will get you to the point where you have connected to your D365 sandbox environment; identified a trigger to start the flow; used the “List Rows” action to return a result data of D365 data; and an “Apply to each” action to loop through that result set one record at a time.

When you are looping, you’ll undoubtedly want to use variables and dynamic content, which is the subject of this blog.

Power Automate Variables

At any point in your flow, you can set a variable.  To do so, you must first add an “Initialize Variable” action.  Note that, very unfortunately, “Initialize Variables” actions must be done at the beginning of a flow, and each one appears as an action block.  For any significant flow, this means that the majority of the first screen you see are boring ol’ “Initialize Variable” actions.  (Power Automate developers, It’d be really nice to have a “Variables” block where you can define ALL of your variables and their initial value in one block.)

But at least we can define, set, and use variables in conditions and in updating CDS record field values.  Again, you start by adding an “Initialize Variables” action.  You then define each variable by its type.

This is a very common flow: Initialize variables, List Rows, and create an Apply to each block in which you define what you do with each record returned by the List Rows action.

A very typical usage flow for variables is to:

  1. Add an “Initialize Variable” action at the top of the flow
  2. Within a loop like “Apply to each”, use “Set Variable” or “Increment Variable”
  3. Finally, at the end of a loop, you may need to “re-initialize” the variable, using “Set Variable”.

Power Automate Dynamic Content (using CDS Data as variables)

You should notice the Dynamic Content window as soon as you click into a step, as the flyout window will display.  (If you don’t see it, believe it or not, increase your screen resolution).

Dynamic Content is one of the key powerful features available in Power Automate.  As the name implies, you can loop through a list of records and do something different based on the field values in the record you are current processing.  You will use Dynamic Content in every significant flow you write.

To activate the Dynamic Content window, you typically have to just expand the block.  You can also click the “Add Dynamic Content” button.

To use a dynamic content variable, just select it from the list.  It will populate the block with that variable name.

You can scroll down the list of fields in the Dynamic content window to see the fields available.  The fields will differ based on the Entity Name in your “List Rows” action.  For example, because I selected “Accounts”, I can see “Account Name” and the Address fields.

Working with this can be a bit tricky.  The easiest way is to know what you are looking for, and enter some part of the field name in the search box.  The list of possible dynamic variables filters based on what you enter.  (In fact, recently, I’ve experienced a bug where if you scroll the dynamic content window, it disappears!  The fix is to search for the field you are looking for, and select it from the list, without having to scroll..  Hopefully this bug will be fixed soon – in fact it looks like it has been.)

Dynamic content allows you to process rows differently, based on the data on that row.

Power Automate Expressions

Similar to Dynamics Content, Expressions give you more advanced ways of assigning value to action block fields.  When you place your cursor into an action block, the dynamic content window displays.  The left tab is for selecting Dynamic Content, the right tab is for entering expressions.  You can combine Dynamics Content and Expressions.

Use Expressions in Conditions

One of the most common uses for expressions is in conditions:
https://docs.microsoft.com/en-us/power-automate/use-expressions-in-conditions

Filter Expressions on triggers (same as workflow)
https://community.dynamics.com/365/b/linn-s-power-platform-notebook/posts/build-efficient-flows-using-filter-expression-in-cds-current-environment-trigger

Common Functions

In this section you will find links to many common functions you will perform in many different flows.

Power Automate Compose Action

The Compose action is Power Automates “in-line debugger”.  It can show you the values of fields and variables at that point in your flow, for the record being processed.
https://powerusers.microsoft.com/t5/Building-Flows/Formatting-for-a-compose-output/td-p/84708

Setting Lookup GUID Value for Dataverse

If you are looking to populate a “foreign key GUID” (like the “Regarding” Contact on a Task, or the Parent Account for the Contact), there is a very specific syntax to use:

<entity name with an ‘s’ appended>(‘GUID string value’).

Fortunately, in your dynamic content should be a field value that is the GUID.  For example, if you select “Contacts” as your “List Rows” entity, and search for “account” in the dynamic content window, you will see:

The “Account (Value)” field is, as the help text shows, the “Unique identifier” or GUID, of the Account that this Contact is linked to. So, if you wanted to create a record and set the “Account” field value to the same account this Contact links to, you would use this syntax:

The ‘accounts’ is the <entity name with an ‘s’ appended>, and then you add open and close parens around the dynamic content value you select.

IMPORTANT!  You can’t just type that into the field you want to set the value to.  Instead type it into the EXPRESSION field area, and let Power Automate format it for you.

More info here:
https://community.dynamics.com/365/b/linn-s-power-platform-notebook/posts/power-automate-how-to-set-lookup-field-value-in-common-data-service-current-environment-flow-connector

Power Automate Format Date/Time

If you use “utcNow” to get the current date/time, the format is not anything you want to display to a user.  This blog post show how to format the date/time in your flows.

Format current date and time to a reasonable looking date in mm/dd/yyy format.

formatDateTime(utcNow(),’g’)

This generates:

11/20/2020 10:24 PM
And it is, of course, in UTC.

Data and Time handling

Get and format the current date and time:

65https://support.microsoft.com/en-us/help/4534778/how-to-customize-format-date-and-time-values-in-a-flow

https://msolenacrm.blog/2020/03/10/how-to-get-it-right-list-records-just-first-record-lookup-value-expression-for-cds-current-connector/

Power Automate Get Record Count

This should be easier (“Number of Records” should be an output from every List Rows block, IMO), but you can get the record count from a List Rows action by writing an expression like:

length(body(‘ListRecordsActionStepLabel’)?[‘value’]

The List Rows, or Get Record, action step label is the only variable in the process.

NOTE: If you have any spaces in your “List Rows Label” then you must replace spaces with underscores

Like: “List_Records_Label”.  Case matters.

See here for more.

https://d365demystified.com/2020/05/20/get-count-of-records-retrieved-in-cds-connector-in-a-flow-power-automate/

Power Automate Terminate Run (Stop workflow):

https://www.cloudfronts.com/terminate-power-automate-run/

Power Automate equivalent to “Wait” workflow action: DELAY

https://d365demystified.com/2020/06/17/pause-a-flow-using-delay-and-delay-until-power-automate/

Common Data Functions

https://blog.ion.works/2019/03/17/common-odata-filters-for-microsoft-flow-a-reference-chart-for-users-of-all-kinds/

Lookup syntax (Referencing a Lookup field in an expression)
https://www.cloudfronts.com/solved-resource-not-found-for-the-segment-in-power-automate/

JSON Syntax

https://www.w3schools.com/js/js_json_syntax.asp
JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

From <https://www.w3schools.com/js/js_json_syntax.asp>

If you like to get into the details, this post explains actions at a very deep level
https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-actions-triggers#compose-action

Deploying flows

When you want to deploy your flow from the sandbox to UAT and production, follow these steps in this helpful blog from Carl De Souza.

https://carldesouza.com/deploying-microsoft-power-automate-flows-from-one-environment-to-another/

You’ve now learned enough to be dangerous!  Before you tell your boss you are a Power Automate guru, you might want to “polish the door” on your solution by adding some error handling, which is the subject of our blog next week!

Please let me know if you have any questions or comments.

QUICK LINK TO ERROR HANDLING, DEBUGGING, & QUIRKS

Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *