cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jesudas
Helper I
Helper I

For Loop

I have a question about For loop in powerapps.
I am trying to insert rows to a table based on a variable
For ex; if the variable contains, 4, then I plan to use patch statement to insert 4 rows to a table
Do you have any examples for a "For" loop
some thing like

for (i=1; i<=4; i++)
{
statement 1;
statement 2;
statement 3;
}

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @jesudas ,

What do these calculations relate to?

If for instance you had a variable called varUnder and a column called Over and wanted Over to be varUnder multiplied by MyCol, then  

ForAll(
   Sequence(4),
   Patch(
      myTable, 
      Defaults(myTable), 
      {
         myCol: Value,
         Over:varUnder*Value
      }
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

9 REPLIES 9
WarrenBelz
Super User
Super User

Hi @jesudas ,

Use the Sequence() Function

ForAll(
   Sequence(4),
   Do whatever
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Thanks WarrenBelz. Let me try this right away. Will come back with my testing results. thank you 🙂

 

I am getting there. Now, I am able to insert 4 rows into my table. But, I am not done yet. I need to do some calculation inside ForAll statement. How can I achieve that.

Working Code:

Set(varRecCount, 4);
ForAll(
Sequence(varRecCount),
Patch(myTable, Defaults(myTable), {myCol: varRecCount})
);

 

What I intend to do:

Set(varRecCount, 4);
ForAll(
Sequence(varRecCount),
Patch(myTable, Defaults(myTable), {myCol: varRecCount})

 

Question 1:

Anyway I can access the current value of varRecCount. In my table it is always inserting as 4. I wanted to access this value such as 1, 2, 3, and 4

 

Question 2:

apart from the patch statement, i wanted to have some more calculations so that I can use those inside my patch statement.  how can I have more statements inside the loop?


);

Hi @jesudas 

If you want four new records with myCol numbered 1 to 4, do this

ForAll(
   Sequence(4),
   Patch(
      myTable, 
      Defaults(myTable), 
      {myCol: Value}
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

Super WarrenBelz. Worked as expected. One last question. As I asked earlier, can we have multiple statements inside ForAll? I need to make some calculations. Using the calculated result, I intend to insert values col2, col3, col4, and col5.

 

any help is greatly appreciated. thanks

Hi @jesudas ,

What do these calculations relate to?

If for instance you had a variable called varUnder and a column called Over and wanted Over to be varUnder multiplied by MyCol, then  

ForAll(
   Sequence(4),
   Patch(
      myTable, 
      Defaults(myTable), 
      {
         myCol: Value,
         Over:varUnder*Value
      }
   )
)

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

View solution in original post

thanks. I was looking to add several inside after patch statement. (business logic) looks like this may not be possible.

But for the moment, I will use your logic for all calculations.

 

thanks much

Hello WarrenBelz

 

Do you know what if I have no certain number how many the variable should be?

It's just different name list containing different number names.

 

For example

Namelist 1 contains: A, B, C, D, E.

Namelist 2 contains: A, E, F.

others

 

I have a dropdown box in a Form to select which namelist should be selected

 

And when I click submit form button, I want to patch this submitted form ID and the selected namelist together and store them in same sharepoint list. Such as:

ID

SubmittedFormIDUserName
115A
215E
315F
416A
516B
616C
716D
816E

 

Do you have any ideas about that? 

Thanks

Hi @iiintro ,

You can use the List (or collection row numbers)

Sequence(CountRows(ListName))

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,789)