Submitting Forms with Patch Function

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

  1. Insert a Button (Insert > Button).
  2. Set its Text property to "Submit".
  3. 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.


Leave a Reply

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