Power Community

Power Community

4, 5 or 7 syntaxes for the Patch function in Power Apps

Did you know that there are 5 different syntaxes for the patch function? Or are there 7, or maybe just 4. In this post all about the various syntaxes for the Patch function.

The 5 syntaxes for the Patch function

The Patch function is most of the times used to create or update a record in a data source. However there are 5 ways of using the Patch function according to the intellisense in Power Apps studio

Patch(source, record)
Patch(source, record, update)
Patch(source, rows, updates)
Patch(source, rows)
Patch(record, update)

For two of these options there are even, slight variations as the update parameters can be repeated.

Patch(source, record, update, update, update, …)

Then when we look at the Patch documentation we seem to be getting not exactly the same details as in the intellisense. So I thought, it is time for a post.

Syntax 1 – Patch(source, record)

This syntax for Patch takes a single record with primary keys and updates values and updates the corresponding row in the specified datasource. It returns the updated row.

4, 5 or 7 syntaxes for the Patch function in Power Apps Microsoft Power Apps image 16

This means that if we have an app with a button that ran the following code, we could create a record in the Accounts table like this.

Patch(Accounts,{'Account Name': "Test Account"})

But if we include the unique key in the update we will have an Upsert function available to us in Power Apps. So if we ran the following line of code:

Set(varMyAccount, 
    Patch(Accounts,
          {accountid: varAccountID, 'Account Name': "Test Account 1"}
   )
)

followed by the following line of code:

Set(varMyAccount, 
    Patch(Accounts,
          {accountid: varAccountID, 'Account Name': "Test Account 2"}
    )
)

Then a single record will be created. The first time we run the patch a new record will be created as there isn’t a record with an accountid matching the GUID hold within the varAccountID variable. In other words this does an Upsert of the the data.

Syntax 2 – Patch(source, record, update)

This is probably the most common way of using the Patch command to update or create an existing record in a data source.

This syntax applies the updates to a specified row and changes the row in the specified data source. It also returns the updated row.

4, 5 or 7 syntaxes for the Patch function in Power Apps Microsoft Power Apps image 17

To create a new record the following syntax is available

Set(varMyAccount, 
  Patch(Accounts,
        Defaults(Accounts),
       {'Account Name': "Test Account 1"}
  )
)

The defaults function in the above will give us a new records in the specified data source

And for updates of existing records the following syntax can be used.

Set(varMyAccount, 
  Patch(Accounts,
        Lookup(Accounts, 'Account Name' = "Test Account 1),
       {'Account Name': "Test Account 2"}
  )
)

Syntax 3 – Patch(source, rows)

This 3rd syntax is an interesting one. So often people use ForAll loops to do updates. however this 3rd syntax handles many updates on selected records in one go.

4, 5 or 7 syntaxes for the Patch function in Power Apps Microsoft Power Apps image 19

So with this syntax the patch function takes a table of rows with pirmary keys and updates fields and applies those updates to the data sources. So imagine a classroom of students that can be updates in one go wihtout having to loop through the students that you want to update.

In the example below we have a collection of records (in the varUpdateThese variable). Then we make updates to some of the records.

And finally we patch the data source with the collection updates made.

4, 5 or 7 syntaxes for the Patch function in Power Apps Microsoft Power Apps image 18

Syntax 4 – Patch(source, rows, updates)

This syntax is offered by the Intellisense however it doesn’t seem to be available.

Syntax 5 – Patch(record, update)

This last Syntax is to update just a single record.

4, 5 or 7 syntaxes for the Patch function in Power Apps Microsoft Power Apps image 20

Notice that the data source where we may have read the data from initially is not being updated in this case.

So we can use this to update a single record.

4, 5 or 7 syntaxes for the Patch function in Power Apps Microsoft Power Apps image 21

So what is the purpose of this Syntax? Well if we think about multiple records updating, what we really are going is merging records. And the following syntax is still perfectly valid.

Patch(record, update1, update 2, update 3, ...)

So if we have the list of updates here and the updates override the previous updates, then a merge of data can be accomplished.

Thoughts on the syntaxes for the Patch Function

So the patch function can be used for simple updates or the creation of records in data sources, but the Patch function is also great to manipulate the data in alternative aways. Like the Merge that we saw in Syntax 5. Using the Patch function correctly can make a massive difference between an app being sluggish and hated or lightning speed fast and liked by users.

Maybe it is time to review some of your apps and see if they can work any faster.

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

5 Benefits of In-App Notifications for Microsoft Dynamics 365 CRM users

For a successful sales process, you need to stay up-to-date with crucial sales information like deal closures, opportunities won,...

More Articles Like This

- Advertisement -spot_img