Back to Basics # 57: 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.

Step 2:

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

Step 3:

After Step 2, under clientAPIReference of CRM – openConfirmDialog method to be used which expects couple of arguments confirmStrings, 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 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.”);

        });

As shown in the below figure

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.”);

        });

    }

}

As shown in the below figure

Step 5:

After Step 4, save Webresource and publish it and publish all the customisations 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 respective messages that are present in the code will be shown like in the below figure.

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 confirmation dialog using  Webresource(javascript).

2 thoughts on “Back to Basics # 57: Understand Confirm Dialog with Webresource in Dynamics CRM

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s