Tonje’s Tech Terrain: Navigating Microsoft 365, Power Platform, and Dynamics

All about M365, Power Platform, Dynamics and how to make them work together without knowing code


Error handling in flows

When you write a flow or use a template, it is easy to just add one action after another and then save. But what happens if one of the actions fails? Nothing. You might get an email about your flow failing, but maybe you use a service account and don’t check the mail very often. Thats one of the reasons why it is smart to add som error handling into your email.

Get the url of the flow instance

It is not so easy to find the error if you have a flow that runs often and maybe have failed multiple times. But there is a magic trick!

When a flow runs, it gets a uniqe url. If you get this url into your error handling, it is so much easier to get back to the right intance of the flow.

Input a Compose as your first action in the flow.

Call the action “Url for flow instance” or something simular. I like to copy the name of the action into notes, so that it is more readable for the future.

Click in the Inputs field and choose Expression. Paste in the code from the box and click OK.

concat('https://make.powerautomate.com/environments/', workflow()?['tags']['environmentName'],'/flows/',workflow()?['name'],'/runs/',workflow()?['run']['name'])

Try – catch

Now that we have the url of the flow, we can start to build the flow. I start by adding the action Scope. Is has no other function than being a container. I name mye Scope action based on what is going to happen inside it.

I now add to more Scope actions inside of the Scope I have created. I call them the same name, but add “Try – ” og “Catch – ” in front of the name.

It is time for the actions you need to build your flow. Add them inside the Scope action that starts with “Try – “.

After you have inserted your actions into the “Try” block you can add the error handling into the “Catch” block. In my case i insert a “Send an email (v2), but you can also insert a chat message for example.

Now we are going to add the url we saved in the compose action. First, you can add a link into the body of the email. Is doesn’t matter what you write into the field for the link target. We are going to change that in a second.

Now, click the “Code View” button

Mark the text inside of the href and click on “Outputs” for the “Url for flow instance” in the Dynamic content.

You now have to fix the “Catch” block so it just runs when the “Try” block fails. Click on the three dots on the right corner of the “Catch” block and choose “Configure run after”.

Remove the mark for “is successful” and mark the other three. Click “Done”. You will now see that the error from the “Try” to the “Catch” is dotted and red.

End the flow if it fails

Often, the actions in a flow depends on the actions that comes before it. Therefore, it can be best to add a “Terminate” action into the “Catch” block – below the email. Set the Status field to “Failed” and enter a descriptive message.

More than one Try/Catch

If your flow has multiple actions that involves different operations, I recomend that you have one try/catch for each block of actions that belong together. This gives you more insites into your code when you recived the error message email.

I hope this helps you out – it has done wonders for me – both for error handling, but also for the readability of the flow.



Leave a comment