Benefits of the Forms control
The Power Apps canvas apps Forms control allows you to spin up a complete form and modify fields shown in almost no time at all. The Forms control also has two properties, OnSuccess and OnFailure. These properties make for very simple error handling because you can easily tell your application what to do after form data is either successfully or unsuccessfully sent to your data source. Additionally, using these properties allows you to improve the user experience of your application by letting the user know what happened to the data he or she tried to save data.
There may be times where you are creating a canvas app and you do not want to use a form. You can create any control a form would have and then save the values to the data source using the Patch function. However, without the Forms control, you lose the ability to trigger off of any properties like OnSuccess or OnFailure.
Return value saves the day!
This is where the return value of the Patch function comes in handy. The Patch function returns the record you just created or modified. It will be blank if there was an error. This record returned can be saved as a variable or used within a With statement if only needed temporarily. You can use this value to alert your users that the action was successful or if there was a problem.
The following example may be used for the OnSelect property of a Button control used to save new customer details to a table. If the row was saved to the data source correctly, the user will be redirected to the application’s home screen. If there was an error, the user will see a custom error message as an in-app notification.
With( { wNewCustomerDetailsRow: Patch( 'Customer Details', Defaults('Customer Details'), { 'First Name': txtCustomerFirstName.Text, 'Last Name': txtCustomerLastName.Text, Email: txtCustomerEmail.Text } ) }, If( IsBlank(wNewCustomerDetailsRow), Notify( "There was an error submitting your request.", NotificationType.Error ), Navigate( 'Find an Employee Screen', ScreenTransition.None ) ) )
Surfacing detailed errors
Without the error handling above, the app could be confusing to end users because there would be no indication that their new request did not save properly. Users may spend too much time trying to track down an item or row that was never added to begin with. However, one thing that could still lead to frustrations is that it’s not clear what the problem actually was. Therefore, one enhancement from here is to use the Errors function. The example below will display the most recent error from the data source the user was attempting to write to, which may help the user resolve the issue on their own or provide some more helpful information to the Power Apps maker.
With( { wNewCustomerDetailsRow: Patch( 'Customer Details', Defaults('Customer Details'), { 'First Name': txtCustomerFirstName.Text, 'Last Name': txtCustomerLastName.Text, Email: txtCustomerEmail.Text } ) }, If( IsBlank(wNewCustomerDetailsRow), Notify( "There was an error submitting your request: " & Last(Errors('Customer Details')).Message, NotificationType.Error ), Navigate( 'Find an Employee Screen', ScreenTransition.None ) ) )
First and foremost, I would like to express my appreciation to the author for sharing this insightful blog post on error handling with the Patch function in Canvas Apps. As a fellow developer, I found the content to be both informative and valuable for anyone working with Microsoft Power Apps.