[SalesForce] Possible to Break Out of a Loop in Flow Builder

Background

I've set up a nested loop, containing two loops in total, in a Flow:

Nested Loop in Flow Builder - Screenshot

If certain conditions are met while iterating through records in the loops, I would like the Flow to exit/break the loop (see A and B above). This is because the Flow has found what it's looking for, and no longer needs to continue iterating through records.

Questions

  1. Is it possible to break out of a loop in Flow Builder? Maybe using
    an Apex Action?
  2. If not, are there any issues with not terminating the outer loop, and
    simply continuing the Flow until it ends?

    • The inner loop must be terminated, otherwise it will not perform any iterations after the initial one. See Tim Shores' comment here.

Notes

  • I wish to break out of the loop after finding what I'm after in order to make the Flow more efficient.
  • I came across an idea related to this. Someone has suggested in the comments that you could break out of a loop by setting up a decision element inside it to ignore the loop element, and continue on with the Flow, which is how I've set up my Flow currently. However, as you may be able to tell, this doesn't actually terminate the loop.

Best Answer

Clearing a collection variable by setting the variable to null (or making it equal an empty collection variable of the same type) will terminate the loop, as there are no more records to process.

I have never tested what happens if you end a flow without completing a loop. I assume that it would be handled gracefully, but it does seem best to terminate the loop by clearing out the collection and therefore ending the flow properly.

Related Topic