Power Apps Patch Function Error Handling
Power Apps Patch Function Error Handling Posted by - Matthew Devaney on - May 9, 2021 49 Comments If you Patch records to a datasource and don’t check it for...
- Published
- Reading time
- 7 min read
Before you start
Is this guide for you?
- Best entry point
- Power Apps
- Time investment
- 7 min read
Imported reference from Matthew Devaney's blog for learning purposes. Original: https://www.matthewdevaney.com/power-apps-patch-function-error-handling/. Author: Matthew Devaney.
Power Apps Patch Function Error Handling Posted by - Matthew Devaney on - May 9, 2021 49 Comments
If you Patch records to a datasource and don’t check it for errors, you are doing it wrong! Why? Patching a record doesn’t guarantee it was successfully added/updated. There might have been a data validation issue , a dropped internet connection , not enough user permissions, or something else. Your users will continue using the app unaware there was a problem potentially leading to data loss. In this article I will show you how to perform patch function Error Handling In Power Apps. Table of Contents Introduction: Test Scores App SharePoint List Setup Patching A New Record Showing A Successful Patch Error Handling With The ERRORS Function Building A More Helpful Error Message Validating A Record Before Patching Introduction: Test Scores App
The Test Scores App is used by teachers at a high school to record student test scores. All tests must have a score between 0 and 100. If the score is successfully saved then the teacher sees a success message. But if there is an error the teacher sees a warning message and is not able to go to the next screen. SharePoint List Setup
Create a new SharePoint list called test scores with the following columns: TestName (single-line text) StudentName (single-line text) Score (number)
Once some data gets submitted from the app it will eventually look like this but you won’t need to create any rows initially.
Scores must be a value between 0 and 100. Edit the Score column and set the minimum/maximum values as shown below. Patching A New Record
Now lets shift over to making the canvas app. Open Power Apps Studio and create a new app from blank and name the 1st screen Submit Test Score . Insert a set of labels and text inputs for the Test Name, Student Name & Score as shown below. Place a submit button beneath them.
Pressing the submit button will create a new record in SharePoint. Write this code in the OnSelect property of the button. You’ll see an error in the navigate function because we haven’t made the Success Screen yet. We’ll do this next.
// create a new record in test scores list Patch(
'Test Scores' , Defaults( 'Test Scores' ), { TestName : txt_TestName.Text, StudentName : txt_StudentName.Text, Score : Value(txt_Score.Text) } ); // go to the success screen Navigate( 'Success Screen' ); Code language: JavaScript ( javascript ) Showing A Successful Patch
When a new record is successfully patched to SharePoint the teacher is allowed to go to the next screen. Insert a new Success screen from the pre-built options. Rename the screen Success Screen .
Now when the submit button is clicked on the Submit Test Score the teacher will be taken to the Success Screen. Error Handling With The ERRORS Function
We only want the Success Screen to show if the new record was successfully created in SharePoint. With our current code that will not happen. If the patch function fails the teacher would still be taken to the next screen. Instead, we want the teacher to see an error message and remain on the same screen so they can make any necessary changes and try again.
We can do this by making the following changes to the submit button’s OnSelect property. First, we use the Errors function to check if there were any problems during the latest patch.
Errors outputs a table so we’ll evaluate it using the IsEmpty function . Then we’ll use the Notify function to display an error message saying “Test score submission failed.”
// create a new record in test scores list Patch(
'Test Scores' , Defaults( 'Test Scores' ), { TestName : txt_TestName.Text, StudentName : txt_StudentName.Text, Score : Value(txt_Score.Text) } ); If(
// check if there were any errors when the test score was submitted !IsEmpty(Errors( 'Test Scores' )), // if true, show any error message Notify(
"Test score submission failed" , NotificationType.Error ), // else, go to success screen Navigate( 'Success Screen' ); ) Code language: JavaScript ( javascript )
A note about how the Errors function works: each time Patch is used on a datasource it error state gets reset. Therefore, Errors only returns errors resulting from the latest operation on the Test Scores SharePoint list. It does not cumulatively track all of the errors flagged while the app is in use. If that’s how Errors works then why does it output table? A table is needed because a single record can have more than one error (example: 2 columns with incorrect values). Building A More Helpful Error Message
We now have an error message in place but it doesn’t tell the teacher why saving the test score failed. A more descriptive error message would help! Fortunately, the Errors function returns a table with these columns containing information about why the operation failed.
Column – name of the column that failed. Message – why the error failed. Error – type of error
Record – the complete record that failed update in the database. This will always be blank when creating a record.
To improve the error message, update the submit button’s OnSelect property as shown below. Notice how the Errors function is now inside a Concat function . This allows multiple error messages to be shown if there are more than one. Patch(
'Test Scores' , Defaults( 'Test Scores' ), { TestName : txt_TestName.Text, StudentName : txt_StudentName.Text, Score : Value(txt_Score.Text) } ); If(
// check if there were any errors when the test score was submitted !IsEmpty(Errors( 'Test Scores' )), // if true, show any error message
Notify( Concat(Errors( 'Test Scores' ), Column& ": " &Message), NotificationType.Error ), // else, go to success screen Navigate( 'Success Screen' ); ) Code language: JavaScript ( javascript )
Input an invalid score and try to submit the form again. Now you’ll see an error message showing why the new record was rejected and what column needs to be fixed. Validating A Record Before Patching
No one likes getting and error message. So its a good practice to prevent the teacher from submitting an invalid record and failing. We can do this by disabling the submit button when the Score value is falls outside of the required range as shown below.
We’ll use the little-known Validate function to make it easy. Validate checks a record against a datasource and returns any error information without trying to create/update the datasource. When there’s no issues, it returns blank .
Put this code inside the submit button’s DisplayMode property. If( IsBlank( Validate(
'Test Scores' , Defaults( 'Test Scores' ), { TestName : txt_TestName.Text, StudentName : txt_StudentName.Text,
Score : Value(txt_Score.Text) } ) ), DisplayMode.Edit, DisplayMode.Disabled ) Code language: JavaScript ( javascript )
We can also take this a step further and highlight the problematic value by using another variation of the Validate function.
Use this code inside the BorderColor property of the text input for Score . You’ll notice this code is working differently from the ‘disable submit button’ code so I recommend you check out the official Validate function documentation for more info on why. If( IsBlank( Validate( 'Test Scores' ,
"Score" , IfError(Value(txt_Score.Text), 0 ) ) ), RGBA( 166 , 166 , 166 , 1 ), Red ) Code language: JavaScript ( javascript )
Also, we’ve used the IfError function in the code above. In order for it to function properly you will need to enable the Formula-level error management setting. It says the feature is experimental, but don’t worry, I’ve tested this setting and its OK to use.
Great! Now we have a way to pre-check records before submission and have created a better experience for the teacher using the app. Keep in mind, even though the Validate function can tell us whether a record is valid it can’t anticipate every type of error. We still need to check for errors when using Patch. Did You Enjoy This Article? 😺
Subscribe to get new Copilot Studio articles sent to your inbox each week for FREE Enter your email address Sign Me Up Questions?
If you have any questions or feedback about Power Apps Patch Function Error Handling please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion. Data Validation Error Handling ERRORS function IFERROR function PATCH function VALIDATE function Matthew Devaney M365 Copilot Power Apps
How To Create Copilot Custom UI Widgets In Power Apps
Tagged
Patch · Power Apps
Have a Microsoft 365 topic idea?
Share article suggestions, community session ideas, corrections, or real-world scenarios for future nextM365 learning notes.
Keep learning Microsoft 365
Explore more practical guides for SharePoint, Power Platform, Copilot Studio, migration, automation, governance, and security.
Continue learning
Related tutorials
Related questions
Related comparisons
Next action