3

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.
7
  • 1
    On the escape path could you clear the collection you are looping over with an assignment element? That would end the loop. I've never tested the implications of not finishing a loop. Imagine it should be fine as the flow would still end.
    – gorav
    Dec 29 '19 at 15:05
  • @gorav - I was able to do this and it worked perfectly, thank you. Could you post your comment as an answer so I can select it as the best answer?
    – M H
    Jan 2 '20 at 3:41
  • glad it worked - how exactly did you clear it - did you set your collection to null? i have not tested this! cheers
    – gorav
    Jan 3 '20 at 13:52
  • 1
    Correct, used an assignment element to set the collection variable (that the loop was iterating over) to null. FYI - this worked even though I was using an automatically-assigned collection variable from the get element (instead of manually assigning variables).
    – M H
    Jan 3 '20 at 15:44
  • @gorav - Could you post your comment as an answer please? I'd like to give credit where credit is due.
    – M H
    Jan 21 '20 at 22:17
2

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.

1

It's definitely possible to break out of the loop with a decision and directing the flow out of the loop, as suggested in the link you provided.

I think in your case you should go directly to Create Record from the 'Applicable SP Role = ..' negative decision, instead of going back into the loop to wait for it to finish all iterations and check the variable after. And your "after last item" of the inner loop can go directly back to the outer one.

Also, I notice you "skip out" of the inner loop when you find something, but don't actually act on it. Which means that as soon as you find something that matches your condition you stop evaluating and might miss records that could make you get to your "create record" element. That may be right, maybe I just don't really get your specific example. Is the record to be created once for each iteration of the outer loop (max) or once in total?

1
  • 1
    Thanks for your answer. The reason why it looks like ignoring the loop element using a decision doesn't actually terminate the loop is because the 'Loop: <Loop Name> End Loop.' event does not appear in the debug details pane when this is done. Conversely, you can tell that clearing the record collection variable (as suggested by Gorav) terminates the loop because the aforementioned event appears when this is done.
    – M H
    Jan 2 '20 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.