7191
Views
13
Comments
Solved
for each loop
Question

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?


2022-05-25 10-40-45
Hugo Duarte
Solution

Hi Prajjwal Rai,


You can still make a normal for loop using if!

2018-08-27 08-29-35
Fábio Vaz

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


2022-05-25 10-40-45
Hugo Duarte
Solution

Hi Prajjwal Rai,


You can still make a normal for loop using if!

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

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.

UserImage.jpg
Prajjwal Rai

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

2019-05-22 11-30-09
Marcelo Ferreira

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

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

So something like this:

2020-02-28 09-46-54
Eduardo Jauch

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.

2020-03-04 16-56-16
Cald3ira81

Eduardo Jauch wrote:

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.

Great "Visual" help for learning how to use correctly a ForEach loop.



2019-03-19 10-45-25
Bayliss12

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?

2020-02-28 09-46-54
Eduardo Jauch

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.

2019-05-22 11-30-09
Marcelo Ferreira

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

2019-03-19 10-45-25
Bayliss12

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


2020-02-28 09-46-54
Eduardo Jauch

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

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