cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BrendonBrooksP1
Post Patron
Post Patron

Collection Best Use Case

After using PowerApps for the last 6 months with varying degrees of success, the one thing I still dont exactly understand is Collections. What would be a good reason to use a collection as opposed to simply Patching the data? Is it more for storing temporary data? Can anyone here cite a good use case with Collections? Thanks. 

 

 

 

 

Brendon

1 REPLY 1
mr-dang
Community Champion
Community Champion

Hi, 

I use collections for many things, but one usage I think that may be useful to you is using collections to store the changes you want to Patch back to the datasource.

 

Sometimes you want your changes saved immediately because the data is sensitive and you don't want things to go wrong. But saving takes time and can interrupt your user if they click too many Patches. So instead of saving one by one, you can save a bulk set of changes/additions to the data source using a collection and the ForAll() function.

 

  1. Collect the changes you want to make to chosen records
  2. Use ForAll() and conditions to either patch a new record or to update an existing record
Collect your changes somewhere:
Collect(changes,ThisItem)

Commit your changes in bulk when you are ready:
ForAll(RenameColumns(changes,"RecordId","RID"),
    If(!IsEmpty(Filter(datasource,RecordId=RID)),
        Patch(datasource,First(Filter(datasource,RecordId=RID)),
            {field: data to change
            }
        ),

        Patch(datasource,Defaults(datasource)
            {field: new data
            }
        )
    )
)

The second formula means, "For each of the records you collected for changing, check if it is already in the datasource. If it's already in there, update the data accordingly, otherwise, write a new record."

Microsoft Employee
@8bitclassroom

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

Top Solution Authors
Top Kudoed Authors
Users online (51,745)