The Power FX Patch function is super flexible! You can use it to create, update, or delete records from all kinds of data sources. Check out some examples below to see how it works in different situations.
Let’s start from the basics
1. Create a New Record
This is one of the most common used Patch function. It allows you to create a new record in a data source.
Patch(Employees, Defaults(Employees), { Name: "John Doe", Age: 30, Department: "IT" })
Explanation: This creates a new record in the Employees data source. It first invokes Defaults(Employees) to create a blank record and then proceeds to fill it by setting fields (Name, Age, and Department).
2. Update an Existing Record
Using the Patch function, it is possible to make a partial update of a record and in other fields there may not be any changes, only the fields indicated are changed.
Patch(Employees, LookUp(Employees, EmployeeID = 928), { Age: 32 })
Explanation: This looks up employee records where the EmployeeID = 928 and updates their age to 32. Other fields remain unchanged.
3. Update Multiple Fields in an Existing Record
The good thing about Patch operation is that it is possible to change multiple fields at the same time.
Patch(Employees, LookUp(Employees, EmployeeID = 928),
{ Name: "Akira 28", Department: "HR", Age: 28 })
Explanation: The fields Name, Department, and Age for the employee with EmployeeID = 928 are updated here.
4. Upsert (Update or Create a Record)
This approach takes care of both cases, where the record has to be updated, and where, there is no record and a new record has to be created.
Patch(Employees,
If(IsBlank(LookUp(Employees, EmployeeID = 928)),
Defaults(Employees), LookUp(Employees, EmployeeID = 928)),
{ Name: "Nirav Raval", Age: 34, Department: "Consulting" })
Explanation: This either updates the record with EmployeeID 928 or creates a new one if it doesn’t exist.
5. Modify Multiple Records
ForAll together with Patch allows the updating of several records at once.
ForAll(
Filter(Employees, Department = "Sales"),
Patch(Employees, ThisRecord, { Salary: Salary * 1.05 })
)
Explanation: This increases the salaries of all employees in the Sales department by 5%.
6. Add Multiple New Records
To note, you can useForAll
and Patch
simultaneously whereby you are able to add several new records in a single call.
ForAll(
[{ Name: "Alisha", Age: 24 }, { Name: "John", Age: 28 }],
Patch(Employees, Defaults(Employees), ThisRecord)
)
Explanation: This creates two new records for Alisha and John in the Employees data source.
7. Bulk Updating Specific Fields Based on Conditions
You can update specific fields for multiple records based on a condition.
ForAll(
Filter(Employees, Age > 40),
Patch(Employees, ThisRecord, { RetirementStatus: "Eligible" })
)
Explanation: This updates the RetirementStatus field to “Eligible” for all employees older than 40.
8. Merge Changes from Multiple Data Sources
You may need to merge records from two data sources, updating fields in one data source based on records from another.
ForAll(
EmployeesA,
Patch(EmployeesB, LookUp(EmployeesB, EmployeeID = ThisRecord.EmployeeID),
{ Salary: ThisRecord.Salary })
)
Explanation: This updates the Salary field in EmployeesB based on matching records in EmployeesA.
9. Patch with a Form
You can use Patch with a form to submit the data entered within the form.
Patch(Employees, If(Form1.Mode = FormMode.New, Defaults(Employees), Form1.Data),
Form1.Updates)
Explanation: This patches the data from Form1 into the Employees data source. If the form is in “New” mode, a new record is created; otherwise, an existing record is updated.
10. Patch with Lookup for Complex Conditions
You can use a combination of LookUp
and Patch
for more complex record queries.
Patch(Employees, LookUp(Employees, EmployeeID = 104 && Department = "IT"),
{ Age: 42 })
Explanation: This updates the Age field for the employee with EmployeeID = 104 and Department = “IT”.
11. Delete a Record Using Patch
You can delete a record using Patch with a blank record.
Patch(Employees, LookUp(Employees, EmployeeID = 105), Blank())
Explanation: This deletes the record with EmployeeID = 105 by setting it to blank.
12. Use Patch to Work with Local Collections
You can also use Patch with local collections in Power Apps, similar to working with standard data sources.
Patch(MyCollection, LookUp(MyCollection, ID = 1), { Age: 32 })
Explanation: This updates the Age of the record with ID = 1 in a local collection named MyCollection.
Notes by Akira28
Importance of Patch While Implementing in Power FX is as follows:
- Generate new records using defaults.
- Update existing records using conditions or look them up directly.
- Upsert to handle both creation and updating.
- Apply bulk structures to modify multiple records.
- Use forms to patch form data into a data source.
- Synchronize information from multiple sources.
- Remove records by setting them to blank.
- Manage local collections with the same syntax used for external data sources.