Hello, and welcome to a new series in which I will review all the functions available in the Power Platform Canvas applications. The purpose of this series is twofold: first, to improve my understanding of these functions, and second, to provide a resource that others will hopefully find useful. These posts will be in no particular order, and some might cover multiple functions. But there will be less of this waffle and more of getting to the meat and potatoes.
Introduction
If you are creating a Microsoft Power Platform Canvas App, it won’t be long until you start thinking about saving data in the application. Power Platform provides multiple functions for doing such a task. In this post, we will take a look at the patch function. What it is, why you need to know about it and some short guides on how to get up and running with the Patch function.
What is Patch?
The patch function is a handy tool in your back pocket when handling data in your Power Platform canvas app. It allows you to create and modify one or many records in a data source. As a developer, it gives you more control and flexibility over the inputs. This is a step up from the ‘Edit Form' control, which can sometimes be the quickest way to add inputs for creating and modifying records in a data source, it can bring limitations to development design when you’re building complex applications.
Creating a record
We’ll start with what is possibly the most common use of the patch function which is to create a new record in a data source:
Patch(WorkLogs, Defaults(WorkLogs),{Title: "Friday Work Log", Outcome: "Fun"})
Let us break this down to understand what is occurring here. The first word following the opening of the patch command is the name of the data source to which the record is being saved. "WorkLogs" used above could be either a Dataverse table, a SharePoint list, or a variety of other data sources.
Next, the command takes the Defaults function as an argument for a new record. This function creates a base record with all the default values for the data source pre-populated. In our example, we want the default values from' WorkLogs'.
Finally between the {} brackets is where data values go to create a record in WorkLogs. Column titles go left of the colon and the values are recorded to the right. In the example, we are populating the Title and Outcome columns.
Updating a record
Using the patch command to update a record is not too different to creating a record, only you have to find the record you want to update first. Though not to fear you don’t have to jump through any pre-requisite hoops to locate the record, patch does it all for us.
Patch(WorkLogs, Lookup(Worklogs, ID = 1), {Title: "Not Friday Work Log", Outcome: "Not Fun"})
As with the create record, the first thing needed in this patch command is the data source, again we have used ‘WorkLogs’ in our example.
The change comes in the next section, as we are not creating a record but updating one we need to first find the record in question. To achieve this a Lookup function can be used, though it is by no means the only way. This function searches ‘WorkLogs’, ID column for a record with the ID value of 1.
Then once we have found the record the patch function goes ahead and updates the Title and Outcome columns.
Creating multiple records
Patch isn’t limited to creating a single record at a time. If multiple records need to be recorded, say from a collection, simply add a ForAll to your patch, and presto: all those records have been written to the data source.
ClearCollect(colWorkLogs,
Table(
WorkLogs@{
Title: "Monday Work Log",
Outcome: "New Week"},
WorkLogs@{
Title: "Tuesday Work Log",
Outcome: "Productive"},
WorkLogs@{
Title: "Wednesday Work Log",
Outcome: "Midweek"},
WorkLogs@{
Title: "Thursday Work Log",
Outcome: "More Productive"},
WorkLogs@{
Title: "Friday Work Log",
Outcome: "Fun"}
));
Patch(WorkLogs,
ForAll(
Sequence(CountRows(colWorkLogs)),
Defaults(WorkLogs)
),
colWorkLogs
);
In this example, we have a collection of WorkLogs called colWorkLogs, in the patch function we use a ForAll function which iterates through each item in the collects and then the patch function creates multiple records in our WorkLogs data source.
Controls and patching
Although we used ‘hard coded’ values during the patch in both examples above, developers are not limited to this. It is very easy to use the information end users input to a canvas app via on-screen controls.
Patch(WorkLogs,
Defaults(
WorkLogs),
{Title: txtWorkLogTitle.Value,
Outcome: cmbWorkLogOutcome.Selected.Value}
)
In the above example, the patch creates a new record in WorkLogs and populates the ‘Title' column with the resulting text value from a text input on the canvas app, and the 'Outcome’ column with the selected value from a combo box.
Updating multiple records
As well as creating multiple records at once, the patch function also allows for multiple records to be updated in one go.
ClearCollect(
colUpdateWorkLog,
Table(
WorkLog@{
ID: 2,
Title: "New Value",
Outcome: "Updated Outcome"
},
WorkLog@{
ID: 4,
Progress: "Active"
},
WorkLog@{
ID: 5,
logDate: Date(2022, 08, 01)
}
)
);
Patch(
WorkLogs,
ShowColumns(
colUpdateWorkLog,
"ID"
),
colUpdateWorkLog
);
In the example above I have some updates for records 2, 4 and 5. The show columns function reduces the collection down to just the necessary column, the ID column is used to identify the records being updated.
Conclusion
As you can see the patch function is a power tool to know and have in your box of tricks, there are plenty of uses for this function from creating one or many new records to updating existing records, with either hard-coded or dynamic values. However, this quick blog post is not exhaustive on the patch function. For more information about the patch check Microsofts docs here.
What are your favourite use cases for the patch function?