Power Apps Patch Function

Did you know how to work with a Power Apps patch function? In this Microsoft Power Apps tutorial, I will explain what is a Power Apps patch function. The two examples below are related to the patch function in Power Apps:

  1. Create a new record using a Power Apps Patch function.
  2. Update the existing record using a Power Apps patch function.

Power Apps Patch Function

The patch function in Power Apps allows us to create or update existing records in any data source. Using the patch function in Power Apps, you can create or modify single or multiple records or merge records in a data source.

Power Apps Patch Function Syntax

Here is the syntax for the Power Apps patch function.

Patch("DataSource",Defaults("DataSource"),{fields});

Where,

  • Defaults = It is used to create a record in the data source.

How to Use Power Apps Patch Function

Here, I will show you every step of how to use the Power Apps patch function using the SharePoint list.

Example,

I have a SharePoint list [Product Issue Tracker], which has different columns like:

Fields NameData type
Product IDTitle
ProductChoice [Washing Machine, Television, Oven, Refrigerator]
Issue Assigned ToPerson/group
Is Still Issue is ActiveYes/no
Issue Start DateDate and time
Issue Resolved DateDate and time
Power Apps patch function

Using this SharePoint list, I will show you how to use a Power Apps patch function to create or update an item.

My Power Apps screen has an Edit form, a Button control, and a Gallery control. The edit form and gallery control are connected to the above SharePoint list.

Whenever the user enters a detail in the edit form and clicks on a button control, that data will be displayed in both the gallery control and SharePoint List. Also, when users need to modify the data, they can click on a particular item in the gallery control, and that data will appear on the edit form.

After modifying that data, if they click on the button control, it will be updated in both the gallery control and SharePoint List.

To achieve it, follow the below steps:

  • Create a Power Apps blank canvas app, and connect the data to a SharePoint list as shown below:
Power Apps patch function using SharePoint Online list
  • Insert a Vertical gallery control -> Set its Items property to:
'Product Issue Tracker'

Where,

  • Product Issue Tracker = SharePoint list name
How to use Power Apps patch function using SharePoint list
  • Now, in the gallery control, it will display the data of a SharePoint online list.
How to use Power Apps patch function
  • Insert an Edit form -> Set its Data Source property to:
'Product Issue Tracker'

Where,

  • Product Issue Tracker = SharePoint list name
Patch function Power Apps SharePoint list
  • Now, the SharePoint list fields will be added to a Power Apps form.
Power Apps patch function using the SharePoint list
  • Save, Publish, and Preview the app. The items will not display in the edit form as shown below:
Power Apps patch function SharePoint list
  • To overcome this, click on the Power Apps edit form -> Set the DefaultMode properties to:
FormMode.New
How to use Power Apps patch function with SharePoint list

Now, we will see how to create a new record using the Power Apps patch function.

Create a new record using the Power Apps patch function

Now, I will create a new item using the Power Apps Patch function.

  • Insert the Button control -> Set its OnSelect property to:
Patch(
    'Product Issue Tracker',
    Defaults('Product Issue Tracker'),
    {
        Title: DataCardValue1.Text,
        Product: {Value: DataCardValue2.Selected.Value},
        'Issue Assigned To': DataCardValue3.Selected,
        'Is Still Issue Is Active': DataCardValue4.Value,
        'Issue Start Date': DataCardValue5.SelectedDate,
        'Issue Resolved Date': DataCardValue6.SelectedDate
    }
);
Power Apps patch function SharePoint Online list
  • Save, Publish, and Preview the app. When a user provides the data and clicks on a button control. That data will be stored in both gallery control and SharePoint List.
Use Power Apps patch function with SharePoint list
How to use Power Apps patch function SharePoint list

Next, I will show you how to update the existing item using the Power Apps Patch function.

Update the existing record using a Power Apps Patch function

Here, we will see how to modify the data based on the selection that the user makes in the gallery control.

Step 1:

In my SharePoint list, there is a Yes/No type column based on Issue is active or not. Now in the gallery control, I should display:

Issue is active Gallery items should be displayed in the “Red color
Issue is not activeGallery items should be displayed in the “Green color
If the user clicks on a particular itemThe gallery item should display in “Blue color”

To achieve it, follow the below steps:

  • Select a Gallery control-> Set the Template fill property to:
If(
    ThisItem.IsSelected,
    Color.LightBlue,
    If(
        ThisItem.'Is Still Issue Is Active' = true,
        RGBA(
            247,
            49,
            49,
            1
        ),
        RGBA(
            152,
            208,
            70,
            1
        )
    )
)

Where,

  1. Is Still Issue Is Active = SharePoint list choice column name.
How to use Power Apps patch function to update existing record
  • Save, Publish, and Preview the app. Whenever the user, clicks on a particular item, it displays in a blue color, and the other gallery items will display as:
    • The issue is active = Red color
    • The issue is not active = Green color
How to use Power Apps patch function with SharePoint Online list

Step 2:

Whenever the user selects a particular item from the gallery control items, I would like that item to be represented in my edit form. To do that:

  • Select the Gallery control -> Set its OnSelect property to:
Set(varIssue,ThisItem)

Where,

  1. varIssue: Provide the variable name
Power Apps patch function with the SharePoint Online list
  • Now, in the edit form fields, I would like to set its Default value as mentioned below:
Default = varIssue.Title                              // For Product ID field

Default =  varIssue.Product.Value           // For Product Choice field 

DefaultSelectedItems = Gallery1.Selected.'Issue Assigned To'  //For  Issue Assigned To field 

Default =  varIssue.'Is Still Issue Is Active'    //For Issue Assigned To field

DefaultDate = varIssue.'Issue Start Date'   // For Issue Start Date field 

DefaultDate = varIssue.'Issue Resolved Date'  //For Issue Resolved Date field 

Where,

  • varIssue = Variable name

I will show one image of the date picker control by setting its default value:

Power Apps patch function
  • Save, Publish, and Preview the app. Whenever the user clicks on a particular item in the gallery control, that item will be represented in edit form as shown below:
Power Apps canvas app patch function using SharePoint Online list

Step 3:

Now in order to modify the data based upon the selection that the user makes in the gallery control.

  • Select the Button control -> Set its OnSelect property to:
Patch(
    'Product Issue Tracker',
    If(
        IsBlank(varIssue),
        Defaults('Product Issue Tracker'),
        varIssue
    ),
    {
        Title: DataCardValue1.Text,
        Product: {Value: DataCardValue2.Selected.Value},
        'Issue Assigned To': DataCardValue3.Selected,
        'Is Still Issue Is Active': DataCardValue4.Value,
        'Issue Start Date': DataCardValue5.SelectedDate,
        'Issue Resolved Date': DataCardValue6.SelectedDate
    }
);

Where,

  1. Product Issue Tracker = SharePoint list name
  2. varIssue = Variable name
  3. Title, Product, and so on... = SharePoint list column names
  4. DataCardValue1, DataCardValue2, and so on…= Power Apps edit form fields name
Power Apps patch function to update existing record
  • Save, Publish, and Preview the app. Whenever the user modifies the data and clicks on a button control.
How to use Power Apps patch function SharePoint Online list
  • The data will be modified in the gallery control as well as in the SharePoint list shown below:
Power Apps patch function with an SharePoint list
Power Apps canvas app patch function SharePoint list

This is how to update the existing record using a Power Apps Patch function using a SharePoint list.

Now, in the form control, the data will remain the same, so to remove it:

  • Insert a Reset icon -> Set its OnSelect property to:
Set(varIssue,Blank());

Where,

  • varIssue = Variable name
Power Apps canvas app patch function using the SharePoint Online list
  • Save, Publish, and Preview the app. Whenever the user clicks on the Reload icon, the data which are in the Power Apps form control will be blank as shown below:
Power Apps canvas app patch function

This is how to use the Power Apps patch function with a SharePoint online list.

Conclusion

I have explained the Power Apps patch function, which allows us to create or modify one or more records using a SharePoint list.

Here, I have shown each and every step of how to create a Power Apps patch using the SharePoint list:

  1. Create a new record using a Power Apps Patch function in the SharePoint list.
  2. Update the existing record using a Power Apps patch function in the SharePoint list.

You may also like: