for each loop

  

Hello all

I was reading about the for each tool and from what I understood, it is used to iterate the elements in a list

Is it possible to use it as a normal loop wher the limit is till a variable?


Prajjwal Rai wrote:

Hello all

I was reading about the for each tool and from what I understood, it is used to iterate the elements in a list

Is it possible to use it as a normal loop wher the limit is till a variable?



Yes, but you can use a IF instead of foreach


Solution

Hi Prajjwal Rai,


You can still make a normal for loop using if!

Solution

Hi Prajjwal,

This is not possible. A For Each iterates over a List or Record List. If you need a "traditional" for loop, you need to create that in code with an If and an Assign.

Why do you need a "normal loop"? What is your use case? In the 8 years I've been developing using OutSystems, I needed that maybe once or twice, for pretty obscure reasons. In normal applications, you'll never need something like that.

Kilian Hekhuis wrote:

Hi Prajjwal,

This is not possible. A For Each iterates over a List or Record List. If you need a "traditional" for loop, you need to create that in code with an If and an Assign.

Why do you need a "normal loop"? What is your use case? In the 8 years I've been developing using OutSystems, I needed that maybe once or twice, for pretty obscure reasons. In normal applications, you'll never need something like that.


Hi Kilian

I have to fetch data using an api call, for which I need to call the api in a loop with the loop variable being an input parameter in the api call

This is my use case, if you know of some other method please do tell how it can be achieved

Hi Prajjwal,

As Kilian said you need to create a counter that start in 1 and than use a if to check if this counter reach your maximum. if not enter the flow of your actions that need to be done multiple times and raise the counter by 1. put the flow ending on the if again where you will check if the maximum was reached. dont forget to add 1 to the counter or you will have a infinite loop.

Regards,

Marcelo

So something like this:

Just for the sake of knowledge, I'll let here some information about the ForEach that may be usefull.

The matter with ForEach is that you are forbidden connect ing the Cycle part with the exit part of the ForEach. So the following is not allowed:

But the following IS allowed.

As you can see above, the CYCLE of the ForEach and its EXIT are isolated.
How can we take advantage of this to execute a conditional looping in a list that can finish BEFORE the ending of the list, without duplicating code?

Passing the looping to inside a ServerAction, like this:

And than using this server action in my screen action (for example).

This way I can use a ForEach that do not run from the start to the end all the times, but can end before, depending on the condition (that can easily be a counter variable, for example).

Hope this helps.

Cheers.