Bypass Apply to each Loop

Have you ever noticed that Power Automate will sneak in that ‘Apply to each loop’ even though you are only expecting a single value.
For example this can happen whenever you are using a Action that ‘Lists’ items, folders, fields, etc.
In this example I will be showing how to get a User ID in CDS with the users Full Name.

Have you ever noticed that Power Automate will sneak in that ‘Apply to each loop’ even though you are only expecting a single value.
For example this can happen whenever you are using a Action that ‘Lists’ items, folders, fields, etc.
In this example I will be showing how to get a User ID in CDS with the users Full Name.

The Problem

Power Automate creates a ‘Apply to each’ loop when selecting dynamic content from a action that Lists items, folder, or anything. In most cases this is awesome and creates a nice smooth workflow.

However, what about when you know exactly what you want to look for and you know its only going to be 1 record, item, whatever is going to be returned. Power Automate will still make you use the ‘Apply to each’ loop.

This may not be a such a terrible thing, but if you need to do multiple things underneath that action, you will have to put them in the loop as well (if you need any data or reference to that action)

The Solution

On to the magic..
In my example Flow I will be using:

  • ‘Compose’ action to have my Full Name stored.
  • ‘List records’ CDS action to list records from the default Users table entity.
    ** Note – This can be done with any connector. **
  • ODATA filter on the ‘List records’ which I am using to filter ‘fullname’
  • Under the ‘List records’ I use a ‘Compose’ action to store the users ID(Primary Key from CDS) and the users Email Address

Step 1 – I am using CDS List records for my example, with a Odata filter

My List Records with my Odata filter
My List Records with my Odata filter

Step 2 – Adding Compose action to use the Expression to bypass the loop

Add a Compose action below the List action. And select Expression

Type anything, this is to keep us in Expression mode when we switch back to Dynamic Content tab

If you see the fx Logo in the Dynamic Content Tab, you have done this correctly

Remove what you had, and Click the Value of the action you want to bypass the loop with

Remove the ? and add [0] This is saying we want the first record only. Since this returns an array we say 0 as this is the first record in an array

after the [0] we type what the field name is, in this format: [‘feildname’]

Click OK.. I usually like to put the Expression in a Comment

This is the exact expression I used in my Compose action

body('List_records')['value'][0]['systemuserid']

DONE!


Limitations:

The only thing you have to watch out for is when there is a empty record. This will cause an error if the record is empty.
This can easily be fixed using a Condition If block before the Compose to check if value is empty using the empty() expression.
OR
If you want to avoid the error altogether, you can use the expression first() instead of body()

I hope anyone finds this useful. This boosts performance greatly when you only need one record since you wont need a Apply to each loop.

Thank you for reading

One thought on “Bypass Apply to each Loop”

Leave a comment