cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

How to stop a "apply to each" ?

We have a "apply to each" loop that we want to stop when a certain condition is found. 
But we can't find any way to stop it. "terminate" isn't working in an "apply to each"

 

I see that there is a "Do until", but it can find how to put the stop condition. 


Basically, our job is like that :

 

Apply to each
Test 1, if true, write the record and stop
           if not true, then Test 2, if true, write the record and stop

                                     if not true, keep going


If it possible to write both tests in the "do until" condition ? Thanks !

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User
Super User

Re: How to stop a "apply to each" ?

Hi @tissanfr 

 

'Terminate' flow action can't be used under For Each Loop.

 

If you planning to use Do Until you need to make sure that condition should meet until it satisfies. If not you could end up on infinite loop.

 

Why not filter the condition for true first do the update. Next filter the dataset for false and do the rest. This way you still use the for each loop. The only issue is you have two for each loop. One for true condition to do the job and next for the false.

 

Hope it make sense. 

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog

View solution in original post

3 REPLIES 3
Highlighted
Super User
Super User

Re: How to stop a "apply to each" ?

Hi @tissanfr 

 

"For each" will always run until the end for all items.

If you want to keep a certain value I recommend having a variable in the "For Each" that keeps the value that you want or filter the array that is sent to the "For Each" so that less values are parsed.

 

If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Cheers
Manuel

Highlighted
Dual Super User
Dual Super User

Re: How to stop a "apply to each" ?

Its a little extra work, but you can use a DoUntil Loop to do your processing.  

1) Initialize a Boolean variable to False to control the Loop

2) Set your DoUntil Loop to run until the Boolean you created is True

3) Use the CurrentIteration value as an Index to the JSON value of the collection you want to process

4) When you find your finish condition set the variable to True.

 

The Tricky part is #3.  If I'm processing the rows in an Excel table then this would be the JSON formula

body('List_rows_present_in_a_table')?['value'][iterationIndexes('Do_until')]

 This accesses a row from the list rows present in a table action based on the current iteration of the loop.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Highlighted
Super User
Super User

Re: How to stop a "apply to each" ?

Hi @tissanfr 

 

'Terminate' flow action can't be used under For Each Loop.

 

If you planning to use Do Until you need to make sure that condition should meet until it satisfies. If not you could end up on infinite loop.

 

Why not filter the condition for true first do the update. Next filter the dataset for false and do the rest. This way you still use the for each loop. The only issue is you have two for each loop. One for true condition to do the job and next for the false.

 

Hope it make sense. 

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog

View solution in original post

Helpful resources

Announcements
firstImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

firstImage

Join the new Power Virtual Agents Community!

We are excited to announce the launch of Power Virtual Agents Community. Check it out now!

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

Top Kudoed Authors
Users online (10,766)