Merging collections in a Canvas app using Patch

On a recent project, I was getting frustrated at having to change the same Patch code many times. Don’t repeat yourself (DRY) is tricky to implement in PowerApps. What follows is the solution I came up with to simplify it.

The app needed to create records of several different types, but with a set of common fields. At first I had a large switch statement, with a Patch for every record type, core fields included. This led to a Patch statement of around 1000 lines. Yes.

I found I could create one collection with the common core fields. I could then build a second collection with the differing fields using a Switch formula.

Once I had the two collections I could merge them together into a 3rd collection using Patch():

Patch(collectionMerged, collection1, collection2);

I then Patched the the first record of this collection to the Datasource:

Patch(DataSource, Defaults(DataSource),First(collectionMerged))

I couldn’t get it working with a record.

This made my formula a lot more readable. It also made it more maintainable going forward. Especially in instances where I have the same code on many buttons.

I’d like to figure out if there is a way of doing this with records. Or if the new named formulas feature would provide some benefits.

Popular posts from this blog

Patching SharePoint Choice columns from PowerApps using 'Value'

Book Review - Get Everything Done and Still Have Time to Play