How to break a foreach cycle?

How to break a foreach cycle?

It’s possible to raise one exception in the cycle, and catch this exception. I think this is not the best method.

I don't see why not. Normally, in programming languages, if you don't have a condition to test iteratively in the cycle (while a=b...) , it is the only way to break it (e.g. escape, exit, raise exception in OutSystems Hub Edition).

The For each in Hub Edition is around a record list so it ends when the record list is empty (or when the max number of records has been reached, which may, by the way, been calculated in an expression).

In the end, I agree with you, it would be nice to have a condition so the flow can continue from that point on
The correct method to break a cycle is to place the cycle in a specific action and use an end node inside the cycle code.

You should only use exceptions for exceptional situations. Using exceptions in the normal code flow is as awfull as using a "goto" in a Visual Basic project because you sudently loose all the context you have and also makes your code unreadable.
Good to know! I never would have known, otherwise!

So it seems that, as you have End nodes inside user actions but you do not have them in screen actions, you may use them to end cycles without getting the verify error "Invalid Flow: The For each action is not well formed in ..."

That is great! I withdrew what I've said before: This is the most gracious way to end a cycle, indeed!
Hello Lúcio,

I'm building an action with a For Each widget to find an Id and return the record. For that i used the "For Each" and "If" widget with the condition that when find's the record Assign´s it to the OutPut variable a goes to then End widget. Doing the verify it result's in a "Invalid Action Flow
Ambiguous paths to End in "Action_Name", could you please tell me what am i doing wrong ?

Best Regards,
André Pereira
Hi André,

you need to use two end nodes. One inside the foreach cycle, and one after the foreach cycle.
It Work's.
André Pereira
Reviving an old topic...
I've come into this problem today. It in fact works perfectly with two End nodes, but only if there are no more actions to perform after the For Each finishes.

For instance, I have two nested For Each loops, where I need to break the inner one after I've found the record I was looking for. In this situation, placing an End node is not an option. The only solution I've found is to set a boolean variable to True, which causes the next iterations of that loop to do nothing (thus it doesn't really break the loop but prevents it from doing anything else).

Is there any other way someone knows about since 2008?
Hi Filipe,

You could create an old-fashon loop; using only if statements.
But I think it's best to manually set the for-each current value to the highest one available so the foreach will break from it's loop.

An alternative would be to move the actions you need to execute after the for-each to a separate action and call that one in both situations.


actually we should have a while-node or be able to add a condition to the for-loop tbh, because it's so often used in normal programming.