Submitting Forms with the Patch() Function in Power Apps – Full Guide
📌 Introduction
The Patch() function in Power Apps is a powerful way to update, add, or modify data in a data source without using the built-in SubmitForm() function. It provides greater flexibility and control over data operations.
Why Use Patch() Instead of SubmitForm()?
✔ Works without using a Form Control
✔ Allows partial updates (update only specific fields)
✔ Supports custom validation and logic
✔ Can be used to update multiple records at once
In this guide, we’ll explore:
🔹 How to use Patch() to create a new record
🔹 How to update an existing record
🔹 How to validate data before submission
🔹 Handling errors and notifications
🔹 Step 1: Understanding the Patch() Function
The Patch() function is used to modify or insert data into a data source.
📌 Patch() Syntax
Patch(DataSource, RecordToModify, NewData)
- DataSource → The table or list where data is stored (e.g., SharePoint, Dataverse, SQL, Excel).
- RecordToModify → The existing record to update (if editing). If creating a new record, pass in Defaults(DataSource).
- NewData → The new values to be applied to the record.
🔹 Step 2: Creating a New Record with Patch()
To create a new record in a data source, use Defaults() with Patch().
✅ Example: Adding a New Employee Record
Patch(
EmployeeRecords,
Defaults(EmployeeRecords),
{
Name: txtName.Text,
Email: txtEmail.Text,
Age: Value(txtAge.Text),
Position: txtPosition.Text
}
)
📌 How it Works:
1️⃣ Defaults(EmployeeRecords)
→ Creates a new blank record in the data source.
2️⃣ { Name: txtName.Text, Email: txtEmail.Text, ... }
→ Assigns values from text input fields.
3️⃣ Saves the new record in EmployeeRecords (e.g., SharePoint List, SQL, or Dataverse).
🔹 Step 3: Updating an Existing Record with Patch()
To update a record, use the Selected Item from a Gallery.
✅ Example: Updating an Employee’s Information
Patch(
EmployeeRecords,
Gallery1.Selected,
{
Email: txtEmail.Text,
Position: txtPosition.Text
}
)
📌 How it Works:
1️⃣ Gallery1.Selected
→ Identifies which record to update.
2️⃣ { Email: txtEmail.Text, Position: txtPosition.Text }
→ Updates only these fields.
3️⃣ Other fields remain unchanged.
🔹 Step 4: Adding a Submit Button for Patch()
To allow users to submit new records, add a Button.
✅ Button Configuration
- Insert a Button (
Insert > Button
). - Set its Text property to
"Submit"
. - Set its OnSelect property to:
Patch(
EmployeeRecords,
Defaults(EmployeeRecords),
{
Name: txtName.Text,
Email: txtEmail.Text,
Age: Value(txtAge.Text),
Position: txtPosition.Text
}
);
Notify("Record added successfully!", NotificationType.Success);
Reset(txtName); Reset(txtEmail); Reset(txtAge); Reset(txtPosition)
📌 Enhancements:
✔ Notify() → Shows a success message after submission.
✔ Reset() → Clears the input fields after submission.
🔹 Step 5: Handling Form Validations Before Submitting
Before submitting data, validate it to prevent errors.
✅ Example: Checking for Empty Fields
If(
IsBlank(txtName.Text) || IsBlank(txtEmail.Text),
Notify("Name and Email are required!", NotificationType.Error),
Patch(
EmployeeRecords,
Defaults(EmployeeRecords),
{
Name: txtName.Text,
Email: txtEmail.Text,
Age: Value(txtAge.Text),
Position: txtPosition.Text
}
)
)
📌 How it Works:
1️⃣ Checks if Name or Email is empty.
2️⃣ If empty, shows an error notification and stops submission.
3️⃣ Otherwise, submits the data.
🔹 Step 6: Submitting Multiple Records at Once with ForAll()
If you need to submit multiple records at the same time, use ForAll().
✅ Example: Submitting Multiple Records from a Collection
ForAll(
colNewEmployees,
Patch(EmployeeRecords, Defaults(EmployeeRecords), ThisRecord)
)
📌 How it Works:
✔ Loops through colNewEmployees (a collection of new records).
✔ Adds each record to the EmployeeRecords data source.
🔹 Step 7: Handling Errors in Patch() Function
If a submission fails, catch errors using IfError().
✅ Example: Error Handling with Patch()
IfError(
Patch(EmployeeRecords, Defaults(EmployeeRecords), {
Name: txtName.Text,
Email: txtEmail.Text
}),
Notify("Error submitting record. Please try again!", NotificationType.Error),
Notify("Record added successfully!", NotificationType.Success)
)
📌 How it Works:
✔ If Patch() fails, an error message is displayed.
✔ If successful, a success message is shown.
🔹 Step 8: Resetting Form After Submission
After submission, clear the form to prepare for a new entry.
✅ Example: Resetting Fields
Reset(txtName);
Reset(txtEmail);
Reset(txtAge);
Reset(txtPosition);
📌 Behavior: Clears all text fields after submission.
🔹 Step 9: Using Patch() with Drop-downs & Radio Buttons
If using dropdowns or radio buttons, reference Selected.Value.
✅ Example: Submitting a Record with a Drop-down Selection
Patch(
EmployeeRecords,
Defaults(EmployeeRecords),
{
Name: txtName.Text,
Position: drpPosition.Selected.Value
}
)
📌 Behavior: Saves the selected value from the dropdown.
🔹 Step 10: Best Practices for Using Patch() in Power Apps
✔ Use Defaults() when creating new records
✔ Use Gallery1.Selected for updating records
✔ Validate required fields before submission
✔ Use Notify() for user feedback
✔ Handle errors using IfError()
✔ Reset() input fields after submission
✔ Use ForAll() to submit multiple records
🔹 Conclusion
The Patch() function is a flexible way to submit forms without relying on the built-in SubmitForm() method. It allows for custom data handling, validation, and error management, making it an essential tool for Power Apps developers.