In Focus

Break Loop (Apply To Each) If A Condition Fails In MS Flow

In this article, you will learn about breaking a loop (apply to each) if a condition fails in MS Flow.

Introduction

 
In this article, I’m going to explain how to stop a loop when a condition fails on a particular stage. For example, if a loop contains 10 repeats with a particular condition in between, and assuming the condition doesn’t meet at the third record, then the loop should stop at the third item and the remaining flow should continue.
 

Procedure Overview

 
  • Create a flow with "apply to each".
  • Start the loop with a particular condition (1) as true
  • Check the required condition (2) for each item
  • If the required condition passes (2), then make the condition (1) as true again, else make it as false.

    Break Loop (Apply To Each) If A Condition Fails In MS Flow
The below action is in the “If Yes”.
 
Break Loop (Apply To Each) If A Condition Fails In MS Flow
 
Then, set the variable as false to break the loop; else set true in the ‘If No’ as below.
 
Break Loop (Apply To Each) If A Condition Fails In MS Flow
 
If the response is not Approved, then make the Variable as true to continue the loop until it gets the "Approved" status in the “If No”.
 

Procedure

 
 
Step 1
 
I have created a flow with apply to each as below.
 
Break Loop (Apply To Each) If A Condition Fails In MS Flow
 
Where the output contains n number of loop items.
 
Step 2
 
Set a condition as explained below.
 
The Condition1: set any Boolean variable as true for the first time to start the loop where the custom Boolean variable is VarStartStop.
 
Then, write the rest of logic in the ‘If yes’ box, since it has the true logic.
 
Step 3
 
Now, set the actual required condition logic in the YES box as below.
 
Break Loop (Apply To Each) If A Condition Fails In MS Flow
 
Where I have an approval process with Approve Option, the required condition is 2 if the response is Approve, then the actions in the Yes box, as shown in the above figure.
 
Step 4
 
If the required condition passes then break the loop by setting the predefined variable as false as below.
 
Break Loop (Apply To Each) If A Condition Fails In MS Flow
 
Now, the loop will break and the rest of the flow will run. If the response is not Approved, then make the Variable as true to continue the loop until it gets the "Approve" status in the “If No”

Conclusion

Simply start a loop with a true condition, then check another condition inside the loop. Now, decide the loop for a break or continue by setting the first condition true or false. Hope it makes sense.
 
Thank you!