Understand Confirm Dialog With Webresource In Dynamics CRM

Introduction

In Dynamics 365 CRM, for certain requirements we need to show confirmation dialog window with Yes and No buttons. We can use openConfirmDialog method present in Client API Reference in Dynamics CRM. As an example, for a selected contact record, confirm dialog will be shown.

Step 1

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the below figure.

Understand Confirm Dialog with Webresource in Dynamics CRM

Step 2

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Understand Confirm Dialog with Webresource in Dynamics CRM

Step 3

After Step 2, under clientAPIReference of CRM – openConfirmDialog method to be used which expects a couple of arguments, confirmStrings and confirmOptions. First we must prepare these arguments and based on user selection Yes or No flow goes inside confirmed portion or else part of confirmation dialog using the below code.

var confirmStrings = {
    text: "This is a confirmation.",
    title: "Confirmation Dialog",
    confirmButtonLabel: "Yes",
    cancelButtonLabel: "No"
};
var confirmOptions = {
    height: 200,
    width: 450
};
Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function(success) {
    if (success.confirmed) console.log("Dialog closed using Yes button.");
    else console.log("Dialog closed using No button or X.");
});

Step 4

After Step 3, keep the above code snippet inside a function and register it on form load event of contact table(entity) form and final code looks like below

if (typeof(ContosoVaccination) == "undefined") {
    var ContosoVaccination = {
        __namespace: true
    };
}
if (typeof(ContosoVaccination.Scripts) == "undefined") {
    ContosoVaccination.Scripts = {
        __namespace: true
    };
}
ContosoVaccination.Scripts.ContactForm = {
    handleOnLoad: function(executionContext) {
        console.log('on load - contact form');
        showDialog(executionContext);
    },
    __namespace: true
}

function showDialog(executionContext) {
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        var confirmStrings = {
            text: "This is a confirmation.",
            title: "Confirmation Dialog",
            confirmButtonLabel: "Yes",
            cancelButtonLabel: "No"
        };
        var confirmOptions = {
            height: 200,
            width: 450
        };
        Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function(success) {
            if (success.confirmed) console.log("Dialog closed using Yes button.");
            else console.log("Dialog closed using No button or X.");
        });
    }
}

Step 5

After Step 4, save Webresource and publish it and publish all the customizations and open a contact record and observe dialog with title and Yes and No buttons and also observe console window after clicking on Yes button and no buttons. The respective messages that are present in the code will be shown like in the below figure.

Understand Confirm Dialog with Webresource in Dynamics CRM

Note

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Microsoft documentation can be found here

Conclusion

In this way, one can easily show a confirmation dialog using Webresource(javascript).