cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Runner55552
Helper IV
Helper IV

Do Until Loop Termination

In a Do Until loop, with a condition (such as a certain variable is greater than 1), at what point does the do Until loop stop?  Does it stop at the beginning of the iteration when the condition is met (and thus does not implement actions below it once the condition is met) or does it see that the condition is met, run the loop once more, and then quit?

1 ACCEPTED SOLUTION

Accepted Solutions
Pstork1
Dual Super User
Dual Super User

In my experience it evaluates the condition at the top of the loop.  So if the condition is true when you enter the loop it will never execute the actions inside 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.

View solution in original post

11 REPLIES 11
Pstork1
Dual Super User
Dual Super User

In my experience it evaluates the condition at the top of the loop.  So if the condition is true when you enter the loop it will never execute the actions inside 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.

It should do the actions once and then move on

 

Do (run the actions) then Until (check the condition)

Thanks for that answer, very helpful. One other question.  Will a Do Until work properly if it is embedded inside an Apply to Each loop?  In my case, I am checking a condition to see if it is true, but I need an apply to each loop for to check each record in a SharePoint list.  I'm not sure if Do Until works properly inside a loop.  See screenshot. "Loop daily list" is an apply to each loop that gets records (items) from a SharePoint list, then uses an item from that list in the do until comparison.  I am trying to get the list loop to exit once the condition is met, to avoid unnecessary repetitions.

 
 

my post below did not seem to keep the screenshot.  Trying again.DoUntilFlow.png

 

From my testing, I have not found a way to exit a loop before all items in the Apply to Each loop are completed. I tried using a Do Until loop with a condition, but as soon as the condition involves multiple items from a SharePoint list, an Apply to Each loop is added, and that loop will not exit until all items have been checked.

My conclusion is that loops which involve checking multiple items in a list or library cannot be exited early based on conditions.  We really need a "break" function.

If anyone has figured out a way to exit a loop (where repeating items are involved), please let the community know.

cerberus
Frequent Visitor

Honestly cannot believe that a "break" hasn't been implemented.

Do until will work fine inside an Apply to Each, but breaking out of the Do Until will just run the next apply to each loop.  There is no way to break out of an apply to each loop without terminating the entire flow.



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

Which is preposterous.

Why is that preposterous?  Its apply to each, not apply to them until I decide differently.  You can easily replace the Apply to each with a do until loop and get the ability to break out of the loop.  Just because doesn't work the way you want it to doesn't make it wrong or preposterous.



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

Apologies - I shouldn't have said that yesterday. 😔 I find the user interface very frustrating to use and my temper got the better of me.

 

I was concerned that using a do until would be inelegant if the condition is never satisfied but I know that we can limit the number of executions. I will try this.

JoshE
Frequent Visitor

I have experienced the opposite, where the loop will run regardless of the conditions being met in actions prior.

 

I tested this by setting up a manual flow that has a Do Until loop with the condition to not run if the variable is greater than 0. I initialized the variable at 1 and ran the flow and the action inside the loop still ran. See attached image as evidence. Loop Testing Proof.png

This suggests that Do Until actions check the conditions after their first run, rather than at the start.

 

The easiest way around this would be to put the loop inside a condition matching that stated in the loop conditions. This is a bit redundant but seems necessary.

 

However, this has proven troublesome in some of my more complex flows where I hit the nesting limit when doing this. In response, the solution I have adopted is to utilize run conditions in actions prior to the Do Until loop. For example, by putting that matching condition action before the do until loop and forcing a failure in the "Yes" box the actions below will be skipped. The way I have been forcing a failure is using the expression formatDateTime(null), but any other should do. You would then need to add a parallel branch that runs on action failure. Finally, the action after the scope containing all this would need to run on both success and failure of the scope. See image below.Do Until loop solution.png

 

 

Admittedly, this solution is convoluted, to say the least, and it makes error handling a bit tricky because unless you specifically address it any errors that happen inside this scope would be ignored and the flow would continue. So I am open to hearing any other workarounds that people smarter than myself may have come up with.

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Power Automate Designer Feedback_carousel.jpg

Help make Flow Design easier

Are you new to designing flows? What is your biggest struggle with Power Automate Designer? Help us make it more user friendly!

Top Solution Authors
Users online (2,653)