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.
