Get up to Speed Quickly on Microsoft Power Automate: “Apply to Each” (Blog 4 of 5)

Power Automate “Apply to each” – Process each record returned by a List Rows action

If you haven’t already read the previous 3 blogs posts, you can read them here:

Blog Day 1 – General Concepts
Blog Day 2 – Triggers
Blog Day 3 – List Rows

After adding a List Rows action to your flow, one of your next actions to add should be an “Apply to each” action.  The Apply to each action creates a block inside of which you specify actions to take on EACH ROW returned by the List Rows action.

Figuring out the ITEMS() expression:

When you use an “Apply to each” action, you use the “items()” expression to refer to fields returned by the List Rows action that feeds data, one record at a time, into the “Apply to each” action.

For example, you can create a condition that evaluates a field value.  To do so, create an “items()” expression to refer to the field.

First ‘use item()‘ post I found:
https://powerusers.microsoft.com/t5/General-Power-Automate/How-to-get-just-the-quot-Value-quot-of-the-quot-Current-Item/m-p/346451#M33612

As soon as you define one “items()” expression, that creates the dynamic variables for all of the fields in the entity referenced by the items() expression.

(To test: set a variable… Try to do it BEFORE any other items() expression has been defined, the fields aren’t there.  Just move the block down below a place where items() was used, and the fields show up…)

To understand where there is a question mark in the items()?[] syntax, read this great blog (it’s for Null handling)

debajmecrm.com understanding-record-count-blog

Create HTML Table to see fields from the List View

Often it is difficult to see what has been returned from the List view.  This will help.

carldesouza.com use-fetchxml-blog

Here’s a bit more on getting data out of List Rows action:
https://msolenacrm.blog/2020/03/10/how-to-get-it-right-list-records-just-first-record-lookup-value-expression-for-cds-current-connector/

JSON

You can do some amazing things using JSON Syntax.  This is stretching the “no code” paradigm a bit, but if you have a need to do some trickier processing, take a look here:

Deep JSON – awesome site. Syntax for using variables inside Record Item for Dataverse update
http://persnicketypowerapps.blogspot.com/2018/10/json-operations-in-ms-flow.html

Full JSON syntax.
https://www.w3schools.com/js/js_json_syntax.asp

Passing content as a variable.
https://powerusers.microsoft.com/t5/Building-Flows/Passing-HTML-content-as-a-variable-to-build-a-JSON/td-p/376505

Alternative to List Rows

While using List Rows is easy and powerful, sometimes you may just need to get one record.  If you know the GUID value (accountid for account, e.g.), then you can get one record by passing in that primary key GUID.

GET a RECORD:

From <http://linnzawwin.blogspot.com/2020/08/get-output-data-from-expand-query-of.html>

If you were thinking you may need to do this to get a “reference” field value from another entity, there is yet another way to get that data, by adding to the “List Rows” action properties.

Use Expand Query to get fields from related entities in a single select.
https://www.cloudfronts.com/how-to-use-expand-query-in-ms-flows/

More efficient than a single ‘get record’
https://saralagerquist.com/2019/11/25/how-to-avoid-the-get-record-action-to-retrieve-related-data-in-power-automate/

Below are examples of how to get the industrycode of the related Account in the Apply to each loop from the List Contact records action:

items(‘Apply_to_each’)?[‘parentcustomerid_account’]?[‘industrycode’]
or
items(‘Apply_to_each’)?[‘parentcustomerid_account/industrycode’]
⚠ Important 

If you are going with the first approach, make sure a question mark ? is added before each object value to return the NULL value instead of an error if there is no relationship (no value in the lookup).

To learn more about using Power Automate variables, expressions, and Dynamic Content, read Blog Day 5

Categories:

Leave a Reply

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