Patch Function for Data Updates

Loading

🔹 Patch Function for Data Updates in Power Apps – Complete Guide

📌 Introduction

The Patch() function in Power Apps is used to create, update, or modify records in a data source without affecting other records. It is a powerful alternative to SubmitForm() when you need more control over data updates.

Why Use Patch()?

  • 🎯 Update or create records without using forms.
  • ⚡ Modify only specific fields in a record.
  • 🚀 Works with Dataverse, SharePoint, SQL Server, Excel, and more.

🔹 Step 1: Basic Syntax of Patch()

Patch(DataSource, Record, Changes)

Breakdown:

  • DataSource → The table (or data source) where records will be updated.
  • Record → The specific record to update (or create if new).
  • Changes → The fields and values to modify in the record.

🔹 Step 2: Updating a Record in a Data Source

Example: Update a Record in a SharePoint List

1️⃣ Connect a SharePoint List (Employees).
2️⃣ Insert a Text Input (TextInput1) for Name.
3️⃣ Insert a Button (UpdateButton).
4️⃣ Set the OnSelect property of the button:

Patch(
    Employees, 
    LookUp(Employees, ID = 5), 
    { Name: TextInput1.Text }
)

📌 Now, the employee with ID = 5 gets their Name updated!

How It Works:

  • LookUp(Employees, ID = 5) → Finds the record with ID 5.
  • { Name: TextInput1.Text } → Updates only the Name field.

🔹 Step 3: Creating a New Record

Example: Add a New Employee

1️⃣ Insert Text Inputs for Name, Age, and Role.
2️⃣ Insert a Button (AddButton).
3️⃣ Set the OnSelect property:

Patch(
    Employees, 
    Defaults(Employees), 
    { Name: TextInput1.Text, Age: Value(TextInput2.Text), Role: TextInput3.Text }
)

📌 Now, a new employee record is added!

How It Works:

  • Defaults(Employees) → Creates a new blank record.
  • { Name: TextInput1.Text, Age: Value(TextInput2.Text), Role: TextInput3.Text } → Sets field values.

🔹 Step 4: Updating Multiple Records

Example: Increase Salary for All Employees in a Gallery

1️⃣ Insert a Gallery (Gallery1) connected to Employees.
2️⃣ Insert a Button (IncreaseSalaryButton).
3️⃣ Set the OnSelect property:

ForAll(
    Gallery1.AllItems, 
    Patch(Employees, ThisItem, { Salary: Salary + 500 })
)

📌 Now, all employees in the gallery get a salary increase!

How It Works:

  • ForAll(Gallery1.AllItems, Patch(...)) → Loops through all records in the gallery and updates them.

🔹 Step 5: Using Patch() with Forms

Example: Save Changes from an Edit Form Without SubmitForm()

1️⃣ Insert an Edit Form (Form1) connected to Employees.
2️⃣ Insert a Button (SaveButton).
3️⃣ Set the OnSelect property:

Patch(Employees, Form1.LastSubmit, Form1.Updates)

📌 Now, the form updates the record without using SubmitForm()!


🔹 Step 6: Deleting a Record Using Patch()

Example: Remove a Record from a Collection

Patch(Employees, LookUp(Employees, ID = 5), { Status: "Inactive" })

📌 Instead of deleting the record, we mark it as inactive!
(For permanent deletion, use Remove() instead of Patch().)


🔹 Step 7: Best Practices for Using Patch()

Always use LookUp() when updating a specific record.

Use Defaults() when creating a new record.

For bulk updates, use ForAll() to iterate over multiple records.

Test before saving changes by using a Label to display values:

Label1.Text = TextInput1.Text

Use error handling to avoid issues:

If(
   IsBlank(TextInput1.Text),
   Notify("Name is required!", NotificationType.Error),
   Patch(Employees, Defaults(Employees), { Name: TextInput1.Text })
)

🔹 Conclusion

The Patch() function is a powerful tool for managing data updates, record creation, and modifications in Power Apps.

💡 Key Takeaways:
Patch() updates or creates records efficiently.
✅ Use LookUp() to find records before updating.
✅ Use Defaults() to create new records.
✅ Use ForAll() for bulk updates.
Error handling prevents data issues.

Now, you’re ready to handle data updates efficiently with Patch()!


Leave a Reply

Your email address will not be published. Required fields are marked *