In this article, we will understand the PowerApps Patch function. We will understand the functionalities and limitations of the Patch function using some simple examples.
Note:
- The Patch function Modifies an existing record or Creates a new record.
- Using the Patch function, you can only update one record at a time or create a single record only.
- You have to use ForAll function if you want to update or create multiple records using Patch function
- To create a record using the Patch function, you need to use the Defaults function to create a base record with default values.
PowerApps Patch function Syntax
Patch( DataSource, BaseRecord, ChangeRecord1 [, ChangeRecord2, … ])
- DataSouce: This is Required. Pass the table or collection or data source on which Patch operation will be fired.
- BaseRecord: This is Required:
- Filter the records which need to modify or create.
- If the record is from the data source, the records will be modified
- If the record is from the Default result, then a new record will be created.
Examples
Table Name: Weathers
PowerApps Patch function - Modify record
- DataSource: Weathers
- Here, we are going to use the Patch function to modify a record.
- As mentioned earlier, you can only update one record using the Patch function.
Patch(Weathers, First (Filter (Weathers, Humidity > 50)), {Temperature: 51});
Output
PowerApps Patch function - Create record
- DataSource: Weathers
- Here, we are going to use the Patch function to create a record.
- As mentioned earlier, you can create only one record using the Patch function.
- Also, you need to use the Defaults function which will create a base record and then update with column values you provide.
- If you don’t specify columns values, then it will take blank values for a new record in those columns.
- You need to provide values for all the mandatory columns.
- In the below example, Name is a mandatory column, so have specified a sample name. you can observe that the record is created with the rest of the column having blank values.
Patch(Weathers, Defaults(Weathers), {Temperature: 33, Name: "Sample Name"});
Output:
PowerApps Patch multiple records using Patch and ForAll
- DataSource: Weathers
- You can update multiple records using Patch in ForAll function or you can use UpdateIf function
- In this example, I have created a collection and, on that collection, I have applied ForAll because the function cannot operate on the same data source that is used in ForAll.
// Create a Collecction
ClearCollect(TempCollection, Weathers);
// Loop through the collection created and patch the records.
ForAll(
TempCollection,
Patch(
Weathers,
First(
Filter(
Weathers,
Humidity > 75 && Temperature <> 97
)
),
{Temperature: 97}
)
)
Output:
PowerApps Update multiple records using UpdateIf
- You can refer below examples for the usage of UpdateIf function
- Let us know if you have any difficulties using the Patch function, we will try to cover them here.