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:
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.
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:
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
Full JSON syntax.
Passing content as a variable.
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:
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.
More efficient than a single ‘get record’
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:
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