Patch and Collect comparison in PowerApps

Patch and Collect share some of the same functionality. Here we discuss what they have in common and when to use one over the other.

 

Patch and Collect in PowerApps - What's the difference?

Patch and Collect can both be used to add a record to a data source. Typically Patch is used to modify entire records or the values of specific fields in a record, however when used with the defaults function it behaves in a similar way to Collect.

The two following code blocks will both add the same record to the ExampleDataSource collection. An important difference is Patch can only add to an existing collection whereas Collect will create the collection if it doesn’t already exist. So use Patch to amend records in existing collections and Collect to define a new collection. Patch is also more suitable for amending a subset of fields in a collection.

Patching to a collection

Patch(
    ExampleDataSource,
    Defaults(ExampleDataSource),
    {
        FirstName: "Joe",
        Surname: "Bloggs",
        DOB: Text(01/01/1970,"[$-en-GB]dd/mm/yyyy","en-gb"),
        StreetAddress: "123 Auckland Street",
        Suburb: "Auckland CBD",
        City: "Auckland",
        Country: "New Zealand",
        JobTitle: "PowerApps Developer"
    }
)

Using Collect to add to a collection

Collect(
    ExampleDataSource,
    {
        FirstName: "Joe",
        Surname: "Bloggs",
        DOB: Text(01/01/1970,"[$-en-GB]dd/mm/yyyy","en-gb"),
        StreetAddress: "123 Auckland Street",
        Suburb: "Auckland CBD",
        City: "Auckland",
        Country: "New Zealand",
        JobTitle: "PowerApps Developer"
    }
)