Configuring Different Actions for onClick of Cancel Button and X Button

By Nikhil Rajendran | Reading time 6 mins

Recently, I was given a requirement to develop a Xrm.Navigation.openConfirmDialog window. The interesting part of the requirement was that it was not meant to be used as the traditional dialog.

The traditional version generally has a “Ok” button and “Cancel” button. OnClick of the Ok button, we can configure some action. OnClick of the Cancel button or the X button, generally the requirement will be to close the dialog.

My requirement was slightly different.  I was asked to create a window that gives the user 2 different options. Both options have different business logic. The logic for on click of the Purchase Orders button must not be triggered on click of the X button.

Initially, my logic was like this:

Configuring Different Actions for onClick of Cancel Button and X Button

let confirmStrings = { confirmButtonLabel: “Invoice(s), cancelButtonLabel: “Purchase Orders”, text: “Select an option to generate invoice from.”, title:”Generate from”};

let confirmOptions = {height: 200, width: 450};

Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(

function (success)

{

if(success.confirmed)

{

// logic for on click of Invoice(s) button

}

else

{

// logic for on click of Purchase Orders button

}

});

The problem with the above logic is that the logic for on click of the Purchase Orders button will be triggered even if you click on the x button. The issue is that Dynamics 365 treats the Cancel(Purchase Orders) and X button as the same.

I started debugging the issue and found out that it was different when you click on the Cancel (Purchase Order) button and X button. It was the value of the confirmed attribute in success.

On Click of Purchase Orders,

success.confirmed = false.

on Click of the X button,

Success.confirmed was undefined.

Hence, I tweaked my code a little bit and met my requirement.

let confirmStrings = { confirmButtonLabel: “Invoice(s), cancelButtonLabel: “Purchase Orders”, text: “Select an option to generate invoice from.”, title: ”Generate from”};

let confirmOptions = {height: 200, width: 450};

Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(

function (success)

{

if(success.confirmed)

{

// logic for on click of Invoice(s) button

}

else if(success.confirmed != ‘undefined’)

{

// logic for on click of Purchase Orders button

}

Else

{

//Logic for on click of the x button

}

});

You can use the above logic to deal with situations where you need to have different logic for on click of the Cancel button and X button in Xrm.Navigation.openConfirmDialog

To know more about such solutions in Dynamics 365, do connect with us.

Download our eBook “15 Questions to Identify the Gaps in Your CRM”

Show Buttons
Hide Buttons