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.

Thanks Edoardo your scheme helped me but now I have another problem.

I have a table with idgroup and iduser I would like to do a check on the duplicates so I thought to use the function for each 2 times cascade the first on group and then on user but how I did not work I have an error how can I do?

Hi,

You can't connect the Cycle part of a ForEach to the exit of it.
And you are doing it through the connection of the exit of GroupMemebersTable.List ForEach that is in the Cycle branch of the GroupMembersTask.List with the exit branch of it.

So, you are connecting GroupMembersTask.List Cycle branch with GroupMembersTask.List Exit.

What you want is to connect it to the first ForEach, I suppose.

Cheers.

Hi,

You have an inner loop looping through the same list as the parent loop. That will not work. Beside why aren't you checking both values on the same loop? Can you explain better what you consider duplicates? Record with both groupId and user id equal or at least one of it?

Regards,

Marcelo

Wouldn't be easier to use an aggregate (os SQLTool) to find the duplicates? 

Marcelo Ferreira wrote:

Hi,

You have an inner loop looping through the same list as the parent loop. That will not work. Beside why aren't you checking both values on the same loop? Can you explain better what you consider duplicates? Record with both groupId and user id equal or at least one of it?

Regards,

Marcelo

Thanks is a good solution I had not thought about it, I was accustomed to other languages and the change of mentality takes time