4

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
    Commented Dec 29, 2019 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
    Commented Jan 2, 2020 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
    Commented Jan 3, 2020 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
    Commented Jan 3, 2020 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
    Commented Jan 21, 2020 at 22:17

4 Answers 4

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
    Commented Jan 2, 2020 at 3:50
0

Yes absolutely possible to break out from a loop. You just use a decision box inside the inner loop, when the decision results True (It might be False as well based on your logic),add an element that connects the decision outside of all the loop (Outer and Inner) and it will eventually exit from both the inner and Outer loop. In the picture below B section connects the decision to an element which is outside of the loops. If condition are not satisfied all the the loop executes number of element in outer loop X number of element in inner loop and take the path A .

enter image description here

0

Yed, but if we redirect from inner loop, again when we go to that loop from parent loop it resumes iteration from where it was terminated by redirecting to parent loop.

It looks like loops are static, doesnt start from entry one when we entered back without terminating or reaching Afterloop path.

New contributor
Mupiri Rakesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .