D365: Implement Try…Catch…Finally in PowerAutomate

Recently, we were working on a requirement to send PDF as an attachment in email on click of a custom ribbon button on Lead record. More details here:
D365: Generate Word Document using Content Control, save to SharePoint and convert to pdf using PowerAutomate
D365: Download SharePoint document, attach to Email using PowerAutomate and Azure Function
D365: Execute PowerAutomate from Ribbon button using JS

As part of this requirement, we also wanted to show form notification based on the execution status of PowerAutomate whether it’s completed successfully or failed somewhere in the middle.
To achieve this, we created a custom option set field(with options In Progress, Error and Successful) on Lead entity to hold the status of execution of PowerAutomate. We tried implementing Try..Catch..Finally blocks in PowerAutomate.

Try Block: This block should contain all the core actions that PowerAutomate needs to accomplish. We used this block to update the custom option set field to “In Progress” at the beginning and “Successful” at the end.
Catch Block: This block contains actions that we need to perform when Try block fails for some reason. We used this block to update the custom option set field on Lead with value “Error”.
Finally Block: This block contains actions that are supposed to be performed irrespective of actions of Try and Catch blocks complete successfully or fail.

Below are the steps to achieve this:

After selecting the appropriate trigger,
–> Click Add an Action
–> Search for scope
–> Select Scope control

Click on Menu.
–> Rename it to “Try”
–> If Scope control is added just after trigger then Configure run After is disabled.

Otherwise, Select Is Successful for the action after which Scope(Try) control is added.

Similarly, add another scope control and name it as “Catch”. For this control, select Configure Run After as shown below since the actions inside this control need to be executed only when actions inside Try block have failed for some reason:

Add another scope control and name it Finally, actions inside which should be executed always irrespective of execution of Catch block.

Finally, power automate will look like this.

If you have existing power automate to be converted to this pattern, then you can use Copy to my clipboard feature to move the appropriate actions into appropriate blocks.

Go to the appropriate scope control, Click Add an Action then move to My Clipboard tab and select the copied action.

By implementing this pattern, we could handle the error if any and update the option set field accordingly.

In JavaScript, we can use the below method to check the status of PowerAutomate after a regular interval of time.

var STATUS_COMPLETED = 320030001;
var STATUS_ERROR = 320030002;
var STATUS_IN_PROGRESS = 320030000;

DXC.RefreshNotification = function (primaryControl) {
    var status = STATUS_IN_PROGRESS;
    setTimeout(function () {
        Xrm.WebApi.online.retrieveRecord("lead", primaryControl.data.entity.getId().replace("{", "").replace("}", ""), "?$select=dxc_powerautomatestatus").then(
            function success(result) {
                status = result["dxc_powerautomatestatus"];
                if (status === STATUS_ERROR) {
                    primaryControl.ui.setFormNotification("Error while generating quote or sending email.", "ERROR", "SendQuote");
                    return;
                }
                else if (status === STATUS_COMPLETED) {
                    primaryControl.ui.setFormNotification("Quote generated and emailed. Refer to activity Timeline and Files tab.", "INFO", "SendQuote");
                    return;
                }
                else {
                    DXC.RefreshNotification(primaryControl);
                }
            },
            function (error) {
                Xrm.Utility.alertDialog(error.message);
            }
        );
    }, 10000);
};

Hope it helps !!

3 thoughts on “D365: Implement Try…Catch…Finally in PowerAutomate

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.