The Patch function in Power Apps is used to create or update records in a data source or a collection. It allows you to modify specific fields of a record or add new records altogether. In this explanation, we will provide a step-by-step guide on how to use the Patch function effectively in Power Apps. We will explore its syntax, discuss the necessary steps, and provide example code to illustrate its usage.
Step 1: Connect to a Data Source or Create a Collection First, you need to connect to a data source or create a collection where you want to create or update records. This could be a SharePoint list, SQL database, Excel file, or any other supported data source. Alternatively, you can create a collection by using the Collect function to store data locally.
Step 2: Add a Control to Trigger the Patch Function Next, add a button or any other control to your Power App’s screen that will trigger the Patch function. This control will be used to initiate the record creation or update.
Step 3: Define the OnSelect Property of the Control Select the control you added in the previous step, and in the formula bar, enter the following code:
OnSelect = Patch(DataSource, Record, UpdateData)
In this code, “DataSource” represents the name of the data source or collection you connected to or created. “Record” specifies the specific record you want to update or create. “UpdateData” is the data or changes you want to apply to the record.
Step 4: Test the Patch Function Run your Power App, and click the control you added to trigger the Patch function. The specified record in the data source or collection will be updated with the new data, or a new record will be created with the provided information.
Here’s an example scenario to illustrate the usage of the Patch function:
Suppose you have connected to a SharePoint list named “Employees” that contains employee records. Each record has fields like “EmployeeID,” “FirstName,” “LastName,” and “Department.” You want to update the department of a specific employee with ID “12345” to “Sales.”
To update the department of the employee with ID “12345” to “Sales,” follow these steps:
Step 1: Connect to the SharePoint List Ensure that you have connected to the “Employees” SharePoint list as your data source in Power Apps.
Step 2: Add a Button Control Add a button control to your Power App’s screen that will trigger the Patch function.
Step 3: Define the OnSelect Property of the Button Control Select the button control, and in the formula bar, enter the following code:
OnSelect = Patch(Employees, LookUp(Employees, EmployeeID = "12345"), {Department: "Sales"})
In this code, “Employees” is the SharePoint list data source name. The LookUp function is used to find the specific employee record with ID “12345.” The {Department: “Sales”} specifies that you want to update the “Department” field of the record to “Sales.”
Step 4: Test the Patch Function Run your Power App, and click the button control you added. The department field of the employee with ID “12345” will be updated to “Sales” in the SharePoint list.
The Patch function is a powerful tool in Power Apps to create or update records in a data source or a collection. By incorporating this function into your Power Apps, you can easily modify data and ensure that your records stay up to date with the latest information.