Skip to content
Home » Patch function in Power Apps

Patch function in Power Apps

The Patch function in Power Apps is used to create or modify records in a data source. It can be used to insert new records, update existing ones, or perform both actions simultaneously.

The Patch function is powerful because it allows you to update only specific fields without needing to update the entire record.

Patch(DataSource, BaseRecord, ChangeRecord)
  • DataSource: The table or collection where the data will be added or updated.
  • BaseRecord: The record to modify (if updating an existing record) or Defaults(DataSource) for a new record.
  • ChangeRecord: A record containing the fields and values to be added or updated.

Inserting new records

As you can see, we have several input controls such as Name, Course Code, Course Name, and Enroll Date, which allow users to input data.




We will insert these records into a SharePoint list called Students and display them in the gallery.

Patch(
    Student,
    Defaults(Student),
    {
        'Student Name': txt_studname.Text,
        'Cource Code': Value(txt_code.Text),
        Course: drp_coursename.Selected.Value,
        'Enroll Date': dt_enrolldate.SelectedDate
    }
)

Let’s enter a record and click the submit button. You will see that it is successfully inserted into the data source.

Updating the records

Let’s say you have a SharePoint list named Students and you want to update the selected student’s record from a gallery based on user input.

Patch(
    Student,
    gal_student.Selected,
    {
        'Student Name': txt_studname.Text,
        'Cource Code': Value(txt_code.Text),
        Course: drp_coursename.Selected.Value,
        'Enroll Date': dt_enrolldate.SelectedDate
    }
)
This formula updates the record that the user has selected in the gal_student gallery control and the record’s fields Student Name, Cource Code, Course, and Enroll Date are updated based on the user’s input from text input, dropdown, and date picker controls.
Let’s select the record for ID 3 from a gallery.

Let’s update the Course Name and Course code field and click on Submit button.

You can see, patch function updates the Course Name and Couse Code values for ID 3.

Insert multiple records using patch function

Let’s assume you have a collection named colStudents containing multiple student records, and you want to insert them into a SharePoint list called Students.

To create this collection, first add a button control and name it Collection. Then, navigate to the OnSelect property of the button and enter the following formula.

ClearCollect(

    colStudents,
    {
        'Student Name': "Sanjay Singh",
        'Cource Code': 586,
        Course: "Bio Tech",
        'Enroll Date': DateValue("2020-09-01")
    },
    {
        'Student Name': "Amar Negi",
        'Cource Code': 510,
        Course: "Computer Science",
        'Enroll Date': DateValue("2021-08-15")
    },
    {
        'Student Name': "Alice Jr.",
        'Cource Code': 589,
        Course: "Mathematics",
        'Enroll Date': DateValue("2022-02-10")
    }
)

Let’s understand the formula:

This formula creates a collection called colStudents with two student records. Then, the ForAll loop will insert both records into the SharePoint list.




Click on Collection button and you can see, it creates a collection called colStudents with three student records.

Now, we will insert these records from the collection into the SharePoint list using the Patch function.

Add a Button control and name it Submit. Next, navigate to the OnSelect property of the button and enter the following formula

ForAll(
    colStudents,
    Patch(
        Student,
        Defaults(Student),
        {
            'Student Name': ThisRecord.'Student Name',
            'Cource Code': ThisRecord.'Cource Code',
            Course: ThisRecord.Course,
            'Enroll Date': ThisRecord.'Enroll Date'
        }
    )
)

Let’s uderstand the formula:

This formula uses the ForAll function which loop through each record in the colStudents collection and inside the iteration it uses the Patch function to insert each record into the Student SharePoint list.

Defaults(Students), is used to create a new blank record for each iteration and ThisRecord is used to refer the current record in the colStudents collection during each loop iteration.

Run the app and click the Submit button. You will see that the records from the collection have been successfully inserted into the SharePoint list.

Using this approach, you can insert multiple records into a data source in Power Apps efficiently.

 

Recommended for you

Collection

ForAll function

Sequence function

Submit multiple records in Power Apps

Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SQL BI Tutorials

Subscribe now to keep reading and get access to the full archive.

Continue reading