How to use Try, Catch and Finally Scopes in Power Automate

An example on how you can use the scope action in Power Automate to handle errors if the flow fails during a run.

Short about the scope action. The scope consists of multiple actions and if all actions within the scope are passed the scope passes otherwise it fails. Using scope allows you to handle errors by having multiple scopes. So if the first scope fails the following scope runs. This is set up by using the Configure Run After setting on the action. We are going through this later in the post.

An example of using a scope for error handling can be; if any action fails you can email the initiator or service desk and inform the requester the result or error in the catch scope, and in the finally scope you can send either way if it succeeded or failed.

In the post, we look at how you can set up an example usage of mainly Try/Catch and short about what finally can be used for.

Step 1: Create a new Cloud Flow. In this example, we just use the “Manually trigger a flow”.

In the demo environment, I have a solution containing all the Cloud Flows in the environment. The image below is taken when the solution is open and the steps below show how to add a new instant cloud flow to the solution.

After following the steps in the previous image the dialog in the image below shows up. Here you can give the flow a name and choose the trigger “Manually trigger the flow”, after clicking create it opens the flow and the trigger is added to the flow. The cloud flow will also be added to the solution it was created in. Follow the next step after the image below.

Step 2: After the flow is created we can start adding the action in the flow. In this step, we add three scope actions and name them “Try”, “Catch” and “Finally”.

After adding and renaming the actions it will look like this.

Step 3: After the scopes are created we need to configure the run after settings of the actions.

The catch scope should be configured to run after the Try scope if it fails.

The finally scope should run even though the flow fails or runs successfully.

Step 4: Within the scope, we need the flow to do something, for instance, list accounts from Dynamics 365/Dataverse. In the example below I set the row count to 3 to avoid listing all the accounts when testing this example.

Step 5: After the List Account action we can use compose to output the Account Name and the Account Id to have a couple of actions within the scope. When adding the name and id of the account it will automatically generate an Apply to each step.

In the compose we can list the name and the id of the account by using the following expressions. Then we have more actions in the scope and more possible error sources. Next, we need to handle the error in the Catch scope.




Step 6: Further we need to add some actions in the catch block if the flow fails.

A way I like to handle the error is to list the actions in the Try scope and create a list with the name of the action and the status.

Step 7: After initializing the variable we need to give the variable and the action a name. I like to name the action with the var keyword and the name of the variable in camel case. Don’t really know why but I find it easy to see that it is a variable. We leave the value empty since we are going to append values to the variable within the Catch scope in the next step.

Step 8: Next step is to add an Apply to each.

Step 9: After adding the Apply to each we need to list all the actions within the Try block, and we can do this by using the expression below and add it in the “Select an output from previous steps” as can be seen in the next image.
result('Try')

Step 10: After adding the apply to each action the next step is to append values from the list of actions into the listOfActions variable. We can do this by using the append to string variable action.

In the append to variable, we want to add the name of the action and the status by adding the expression below. Make sure to add an empty line under the Action information so that it creates space between each of the actions listed in the Try block. An example run is shown a little later.

Action Name @{item()?['name']}
Action Status: @{item()?['status']}

Step 11: To output the result from the append action to listOfAction we can create a compose that list the values outside of the apply to each loop. By adding the variable in a compose action.

This will look like this when the Power Automate fails in the Try scope and list all the actions in the catch scope.

Step 12: The list of actions in the variable can be used to send an email notification to the service desk or system administrator to give them an indication of what action failed in the Power Automate flow. I skipped the email-sending action in this post since the trial user doesn’t have an active license for sending emails.
(To create a Dynamics Customer Engagement Trial, check out this post: How to create Microsoft Dynamics 365 Customer Engagement Trial instance.)

Step 13: To test the catch block you can add a typo in the column field in the List Accounts action to make the flow fail and continue to the catch block. Like below.

Step 14: In the finally action you can use the same principle as in the catch action and send an email if the flow fails or succeeds. Or just output the result of the actions as in the example above to see the result when opening a flow run.

Hope you found it valuable.


For More Content See the Latest Posts