3 Power Automate Error-Handling Patterns You Must Know
It is important to include error-handling in every Power Automate flow. When a flow fails, the owner should be notified that the flow failed. The message should say why the flow failed and identify the flow run. In this article I’ll share 3 Power Automate error-handling patterns you can re-use in any flow you build.
Table Of Contents: Error-Handling Pattern #1: Try, Catch Finally Pattern • Try Action • Catch Action • Finally Action • Test The Try, Catch, Finally Pattern Error-Handling Pattern #2: Terminate Flow Success or Failure Pattern • Test The Terminate Flow Pattern Error-Handling Pattern #3: Get Flow Run Details URL Pattern • Build The Flow Run Details URL • Send Failure Notification Email With Flow Run Link
Error-Handling Pattern #1: Try, Catch Finally Pattern
We will build a basic flow to learn the first Power Automate error-handling pattern. This pattern, called try, catch, finally, is found in many other programming languages.
- Try – attempt to execute a flow action
- Catch – if the flow action fails, do this action to handle the error
- Finally – run this action regardless of the result
Create a new flow with an instant trigger and add a Compose action.
Then write this expression inside the Compose action to cause an error. Since the number 1 cannot be divided by 0 the flow action will fail.
Next, initialize a variable called varFlowRunFailed to track if the flow failed.
We only want to run the initialize variable action to if the previous action failed. Click the three dots on the initialize variable action and select Configure run after.
Setup the configure run after options as shown below. If the previous flow action fails, the Initialize variable action will run. Or if the previous flow action succeeds, this action will be skipped.
Insert an Office 365 Outlook: Send an email (V2) action to send a notification that the flow completed.
The last step on our Try, Catch, Finally pattern will always run regardless of whether the Compose (try) action succeeded or failed. Go to the configure run after menu and check all of the checkboxes.
Test The Try, Catch, Finally Pattern
To see how the try, catch, finally pattern works, give the flow it a test run. The Compose (try) action will fail, the Initialize variable (catch) action handles the error and the Send an email (finally) action runs afterwards.
Error-Handling Pattern #2: Terminate Flow Success or Failure Pattern
When the test flow run is completed we see the notification “your flow run successfully” appear. This is not true. Our flow’s Compose action failed so the notification should say “flow run failed” instead. Next we’ll improve our Power Automate error handling to show the proper result.
Remove the Send an email (V2) action. Then replace it with the Condition action where the variable varFlowRunFailed is equal to true.
Check all of the checkboxes in the configure the Condition action’s run after settings.
If varFlowRunFailed equals true, send an email notification that the flow failed. Then insert a Terminate action afterwards. Set the Terminate status to Failed.
Otherwise, when varFlowRunFailed is not equal to true send an email and terminate the flow successfully.
Test The Terminate Flow Pattern
Run a test of the updated flow. Now the flow properly shows the message “flow run failed.”
Error-Handling Pattern #3: Get Flow Run Details URL Pattern
When a flow fails we want to inspect the flow run details to see why it failed and fix the problem. Finding the specific flow run that failed can be difficult in Power Automate. To make it easier we can send a link to the flow run details in the failure notification email.
When the email recipient opens the link they are navigated to the flow run details where they can quickly identify what went wrong
Build The Flow Run Details URL
Go to the flow’s If yes condition an add a Compose action.
Use the workflow function to get details about the workflow itself.
Then insert another Compose action to build the flow run URL.
Flow expression #1 gets the environment’s unique identifier.
Flow expression #2 extracts the flow’s unique identifer.
Flow expression #3 finds the flow run’s unique identifer.
Send Failure Notification Email With Flow Run Link
The final step is to include the flow run url as a clickable link in the failure notification email.
Write this HTML code in the body the email and use the Compose action inside the a tag.
<a href="outputs('Compose:_Get_Workflow_URL')"&>Click here</a>to check the flow run details
Now the failure notification email contains a link to the flow run.
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps articles sent to your inbox each week for FREE
If you have any questions about 3 Power Automate Error-Handling Patterns You Must Know 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.