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