92
Views
3
Comments
Solved
How to exit a foreach loop early?
Application Type
Reactive
Service Studio Version
11.55.1 (Build 63862)

Hello!

I have the following flow on my application: 


I want to exit the loop when my variable "TipoDocumentoJaInserido" is set to true. I've already tried the solutions that were suggested on these posts:

https://www.outsystems.com/forums/discussion/22031/is-there-any-way-to-exit-the-for-each-loop-before-the-loop-ends/

https://www.outsystems.com/forums/discussion/8314/breaking-a-foreach-loop/

https://www.outsystems.com/forums/discussion/78053/how-can-i-break-my-foreach-loop/

https://www.outsystems.com/forums/discussion/53065/use-if-to-loop-or-foreach-when-i-might-be-leaving-the-loop-earlier/


But none of them seem work due to my need to append data to the list that is being itered. Is there another way to break the loop?

UserImage.jpg
Leandro Ribeiro
Solution

Hi Vinícius

You can try to put the loop inside a action and there you can break the loop earlier like this:

Then you can use the action in your logic like this: 

You can adapt this logic depending on your needs. 

2016-04-22 00-29-45
Nuno Reis
 
MVP

Hello.

There are only two ways.

1: create a new action. You can use End in the middle of the ForEach to return to the original.

2: use an ad-hoc loop. Careful with the differences (check here)


Note: I had the idea that we could somehow manipulate the maximum iterations property, but I just tried it and it didn't work so probably I'm confusing tools.

UserImage.jpg
Leandro Ribeiro
Solution

Hi Vinícius

You can try to put the loop inside a action and there you can break the loop earlier like this:

Then you can use the action in your logic like this: 

You can adapt this logic depending on your needs. 

2024-09-09 08-44-46
BHUPENDRA SINGH

Hi,

You can use ad-hoc loop like below:

1. check LoopIndex (local variable to hold index) with YourListName.List.length and loop until the value of LoopIndex is less than YourListName.List.length.

2. fetch the column value from list using  YourListName.List[LoopIndex].ColumnName and check with if condition.

3. if condition "False" then increment index like LoopIndex =LoopIndex +1 and loop continues.

4.  if condition "True" then increment index like LoopIndex = YourListName.List.Length to make loop condition false and exit the loop.


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.