cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
guyinazo
Level 8

ForEach item in a gallery

Is there a way to iterate through each item in a gallery and set one of it's rows values?  I have a list of people where due to business rules, only ONE can be activated at a time (I have a column in the SQL table for active).  In the list, I have a red X (inactive) and a green check mark (for active).  All the user needs to do is to click on the icon and it changes it in the sql database (to either active or inactive) and also displays the correct icon.  This all works, but now I need to iterate through the list and what I want it to do is to set that Active flag to Inactive on all that were not selected.

 

Is there a 'ForEach' routine or what would be the easiest to accomplish this?  And I am sure I would use a Patch statement to update the SQL table.

 

Brad

1 ACCEPTED SOLUTION

Accepted Solutions
mr-dang
Level 10

Re: ForEach item in a gallery

Hi @guyinazo,

The ForAll() function lets you loop actions. But for your use case you may have some easier options available like UpdateIf.

 

I would set the OnSelect actions of the X and checkmark to set a variable to what's selected in the gallery:

 

Set(selectedItem,ThisItem)

In the same button, have these actions:

 

If you click the check mark:

UpdateIf(dbo.datasource,1=1,
    {
        flag: If(Id=selectedItem.Id,"Active","Inactive")
    }
)

This means, "Update all records in the datasource (1=1 returns true for all records) so that if the Id of the record matches the selected record, make it active, else make it inactive."

 

If you hit delegation issues with UpdateIf--like you have more than 500 people, you can use ForAll() with Patch()-- let me know if that is the case and I can write up a sample. But there might be a few samples on this forum if you search for ForAll and Patch.

 

Brian

Microsoft Employee
@8bitclassroom

View solution in original post

4 REPLIES 4
Microsoft dinusc
Microsoft

Re: ForEach item in a gallery

Hello Btrad,

 

I would suggest setting the "Active" flagg according to the ThisItem.IsSelected flag in the gallery.

mr-dang
Level 10

Re: ForEach item in a gallery

Hi @guyinazo,

The ForAll() function lets you loop actions. But for your use case you may have some easier options available like UpdateIf.

 

I would set the OnSelect actions of the X and checkmark to set a variable to what's selected in the gallery:

 

Set(selectedItem,ThisItem)

In the same button, have these actions:

 

If you click the check mark:

UpdateIf(dbo.datasource,1=1,
    {
        flag: If(Id=selectedItem.Id,"Active","Inactive")
    }
)

This means, "Update all records in the datasource (1=1 returns true for all records) so that if the Id of the record matches the selected record, make it active, else make it inactive."

 

If you hit delegation issues with UpdateIf--like you have more than 500 people, you can use ForAll() with Patch()-- let me know if that is the case and I can write up a sample. But there might be a few samples on this forum if you search for ForAll and Patch.

 

Brian

Microsoft Employee
@8bitclassroom

View solution in original post

guyinazo
Level 8

Re: ForEach item in a gallery

Brian,

 

Thank you!  There are always functions I dont know about and learn every day.  This works just fine the way it is written.  There will never be 500 people in the table.  Maybe 20 at most.  Is there a list of Power Apps functions someplace as an easy reference?  And also now if I could get a spinner that says "working".  I know we have the little dots on top the screen.  I wonder if I can use Notify for an informational message.

 

Brad

mr-dang
Level 10

Re: ForEach item in a gallery

Hi @guyinazo,

I have a series started on this particular need.

 

Here's part 2 that goes into the importance of the Formula Reference:

https://powerapps.microsoft.com/en-us/blog/learning-with-mr-dang-transferring-knowledge-1/

 

Some direct links to the references themselves:

Formula Reference

Formula Checklist - Excel

Formula Checklist - OneNote

 

If you have dots running on the screen, it's likely you used SubmitForm, Patch, or Collect to call data. In that case, you may want to run Notify() ahead of those actions.

 

I recently responded to a question about the "marching ants"--you might find the answer in the recent list of topics 🙂

Microsoft Employee
@8bitclassroom

Helpful resources

Announcements
thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors (Last 30 Days)
Users online (5,675)