Exit ‘Apply to Each’ when condition is met in Power Automate

Using an Apply to Each loop is extremely common, and we know the purpose of it; Looping through as many “things” (items, records, etc) as we have. But sometimes, we need to exit this Apply to Each as soon as a condition is met. So how do we do that? Well, that’s today’s blog 🙂

Scenario

In our scenario, we need to exit the Apply to Each loop if the person who created an item in SharePoint is also the approver in the defined process.

Variables

Let’s start by talking about the variables we’re going to need. The most important one is the one which is going to get us out of the loop.

And the second one is a variable you surely had in your process already. It will contain all your Team members to loop through!

  • varTeamMembers: Type Array – Initialise as an empty array, then use during your process to append members!
  • varApproverIsRequestor: Type Boolean – True or False. Initialise it as false.

Loops

We know we’ll have an Apply to Each to go through all our varTeamMembers. But when you think about it, this is going to continually loop, and do “something” for all the users.

What we want is to exit straight away as soon as we find a user in the team, which is also the creator of the request (my column is called “Raised By”).

For this, we’re going to use a Do Until loop. And this is where the varApproverIsRequester comes into play. Remember that we initialised the variable as False? Now we’re going to Do Until this is True.

Use an Apply to each to loop through each member and inside we’ll have the Do Until. And when the condition is met (Team member IS also the creator of the request – “Yes” branch), we set our varApproverIsRequester to true.

So this means that, when a user (aka Team member) is equal to the “Raised By”, do whatever is in the “Yes” branch, and set the varApproverIsRequester to true. Next loop will not occur because the Do Until will also be true.

Thanks for reading! 🙂

One thought on “Exit ‘Apply to Each’ when condition is met in Power Automate”

Comments are closed.

Discover more from Veronique's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading