cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Advocate V
Advocate V

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
Highlighted
Community Champion
Community Champion

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
Highlighted
Microsoft
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.

Highlighted
Community Champion
Community Champion

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

Highlighted
Advocate V
Advocate V

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

Highlighted
Community Champion
Community Champion

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
Community Conference

Power Platform Community Conference

Find your favorite faces from the community presenting at the Power Platform Community Conference!

Watch Now

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (6,707)