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 III
Super User III

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

Power Automate Video Tutorials

View solution in original post

3 REPLIES 3
Highlighted
Super User II
Super User II

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 III
Dual Super User III

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 III
Super User III

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

Power Automate Video Tutorials

View solution in original post

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

Top Solution Authors
Top Kudoed Authors
Users online (7,841)