Use Of Patch Function In PowerApps

In this article, we will learn about the Patch function and how to use it in PowerApps.

Patch function is used to create or update single records or a list of records in the Data source. Using this, values of fields can be modified without affecting other properties. The patch can be used to save data across multiple screens. While working with complex types Patch is very helpful. Let's take an example

Step 1

Create a list in SharePoint named Employees with fields and Data Type

  • Name - Single Line of Text
  • Address  - Multiple lines of Text
  • DOB - Date and Time
  • Age - Number
  • Salary - Currency
  • Profile - Hyperlink

Step 2

Go to your PowerApp. Add Label and Textbox Control for Name field to the Screen

Add Label. After adding it to screen select Label and change its Text property from Property Pane

Add Textbox from Insert menu. After adding Select text input and Change its Default and Hint Text property

Same way to add other Labels for Age and TextInput for Age. Just need to change the Format property to Number

For Address add another label and rich text input

For Date of Birth, add Date picker control from the Input menu

Add Linkedin profile URL text field and update property

For Salary, add label and TextInput of Number format

Step 3

Now add 2 more buttons for Save and Cancel on the form

Step 4

Now on click of the Save button, we will write our Patch code

For the Number and Currency field we need to convert the text to number using the Value() function. To save the Date and Time field, it will use controlname.SelectedDate

Syntax of Patch

Patch(source,record,update,update,......)

Here source refers to the List on which the operation needs to be performed

Record includes the data on which update needs to be done if any. If using Patch we want to create a new record required Defaults function.

The update refers to the actual record going to add/update. It includes the record in curly braces {} and can be in any number separated by a comma.

Like in the example below,

Patch(Employees, Defaults(Employees), 
{ 
         Title:txtName.Text, 
         'Date of Birth': txtDate.SelectedDate, 
         Address:txtAddress.HtmlText, 
         Age:Value(txtAge.Text), 
         Salary:Value(txtSalary.Text), 
         Profiles:txtProfile.Text 
});

Step 5

On click of cancel button, Add Navigate(HomeScreen)

Step 6

Run the Screen and Add details,

Step 7

After filling in details, click on save. Now navigate to the list and look at data inserted in the SharePoint list.

Step 8

Now add one more screen that includes Gallery which will Display all data from the Employees list. New Controls > Add Vertical Gallery

Step 9

Now after inserting Gallery, Select Gallery > Right Panel select Data Source as Employees

Step 10

Add New icon and on OnSelect property, add Navigate() function with formMode - New

Step 11

On select of Next Arrow icon > Add Navigate() function with formMode - Edit

Step 12

Go to EmployeesData screen > Set Gallery1.Selected.<Field name> to all text values

Like this way set for all other controls.

Also set conditions in the header to display Edit or New Employee.

Step 13

Now open on click of Save button and update Patch function as below,

Patch(Employees,If(formMode="New", Defaults(Employees),LookUp(Employees,ID=Gallery1.Selected.ID)), 
{ 
     Title:txtName.Text, 
     'Date of Birth': txtDate.SelectedDate, 
     Address:txtAddress.HtmlText, 
     Age:Value(txtAge.Text), 
     Salary:Value(txtSalary.Text), 
     Profiles:txtProfile.Text 
}); 
Navigate(HomeScreen);

Step 14

Now let's run the application, click on the next arrow to navigate

Step 15

Update All fields with some separate data and click on save

Step 16

Now you can see updated data

So in this way, we can use the Patch function to create and update records in the SharePoint list using the same screen. We can update any number of records using Patch.

Hope this helps.Happy low coding..!!!


Similar Articles