How to Break a for each cycle

How to Break a for each cycle

  
Hi,

I was under the impression I could break a For Each cycle with an End node. It appears I am wrong. 
In the image below, when "B" is verified the cycle is, in fact, interrupted but the assign is not done. 
I was almost sure the flow would continue to the assign node.





I would like to know your opinion on this.

Thanks,
João
Joao -

This cannot be done. Best thing to do is to have a boolean that says "is done?" check that first thing in the For and cycle back if it is true. I know, it's not ideal. :(

J.Ja
If your intention is to stop the loop, I think you can create raise an exception and put additional logic there.

Otherwise, during the loop, you can keep track of the condition, point back into the loop to let it finish and then after the for each you can do additional processing.
Gerry -

Exceptions as a form of flow control are kinda ugly. :( I understand why you would do it... but it's not a route I'd recommend.

J.Ja
I agree with you. I think it depends on the circumstance. Perhaps if there was the need to iterate through a lot of records and you wanted to bail out, that would typically be a 'error' condition anyway and an exception might rightfully be warranted. Normally, you would not want to put yourself in a position where you had to this and want to get out only because you found what you were looking for. So, maybe in Joao's case, another approach to processing the data is really warranted.
What I usually use (for example to search a record in a recordlist) is to refactor it into an action. That way you can just do the end node to break the cycle.

Regards,
João Rosado
If I have a LONG list that I'm iterating, and I know I will need to break out often... I replace the ForEach with an If + a counter variable to mimic a Do/While loop.

J.Ja
João is right. The way to break out of the for each loop, is putting the loop in a separate action and use the end node to end the action and the loop.
Creating a loop with ifs, like Justin says, is also possible off course.
It could be a good idea to also have a look at the list you're looping. If it is coming directly from a query, then maybe you can narrow down the query so that it returns less rows, or only the ones you need.
still a while-loop would be nice.

the if construction can be done but is VERY dangerous. if you forget to increase the counter, if the check is not correct you end up in an endless loop.

if outsystems provides it with a while-action where you can set the condition + max iteration life will be so much better :)

The if loop isn't *that* dangerous, unless you hit "publish" and then immediately send to PROD. I write an inifnite loop once every few months and it always gets caught within a few minutes in the DEV environment.

And yes, I agree 100% on the need for a built-in while action. :)

J.Ja
I fully agree with the last coment of Justin James
Hi João,

You can achieve a Do While using the following pattern:


Using a similar pattern you can do a cycle that allows you to remove elements from the interacted list (the for-each doesn't).

How to remove elements from the interacted list:


Note that the Index needs to start on Length - 1 and be decremented in each loop.
Hello everyone,

first of all, let me thank you for your opinions on this matter. Second, the title I chose wasn't the most fortunate. It should have been "Breaking a for each cycle". I normally use some of the methods already mentioned, but when I tried to use the end "End" node I saw it was not possible. Has João Rosado said, if you want to use this aproach, the best option is to encapsulate it in an action. 

I agree with Justin and Alberto, when they say a built in "While action" would be nice *wink*

As to the suggestions of Ricardo, I tend to avoid indexing lists as much as I can. 

Cheers,
João