How to Force Exit from Loop In Salesforce Flow (Video 3:26)
When working with a loop in Flows in Salesforce, at times you may want to exit the loop without processing all the records.
We recently had one such requirement for our internal implementation where we wanted to assign discount coupon codes to a bunch of contacts and to exit the loop based on certain conditions, without processing all the records in the loop.
This video is based on the solution that we came up with from our own implementation.
How It Works
The sample flow gets all Contact records, loops through them and increments a Loop Counter on each pass. Without any change, the loop runs once per record. To stop it after five records:
Inside the loop, add a Decision element.
Define a Yes outcome for your exit condition — here, Loop Counter is greater than or equal to 5. Rename the default outcome to No.
On the Yes path, click the + icon, choose Connect to element and select an element outside the loop (the final Screen element in the demo).
Save and debug. The flow still finds all the records, but the loop runs only five times before moving on.
Replace the counter condition with whatever your business rule needs — for example, stop once you've run out of coupon codes to assign.