Tuesday, October 25, 2016

Xrm.Utility functions : Dialogs

There are useful functions in Xrm.Utility object. These functions are not directly related to current page. 

Dialogs:
In older version of CRM, if you want to show alert or confirm dialog, then we have to JavaScript alerts / confirm dialog, but now there are two functions in Xrm.Utility which will give same functionality. 

When we want to show alert message on form or button click we can Xrm.Utility object. 

alertDialog
alertDialog we can use to show just message or when dialog closed, we can call any javascript function. 

Xrm.Utility.alertDialog(message,onCloseCallback)

Example:

contactScript =
{
    NoTaskCreated: function () {
        Xrm.Utility.alertDialog("No Task created.", contactScript.TaskDialogClose);
    },
    TaskDialogClose: function () {
        Xrm.Utility.alertDialog("Dialog Closed");
    },
};

Here in above example, in NoTaskCreated  function calling alertDialog with callback function(contactScript.TaskDialogClose) which will be called when user closes this alert dialog. And TaskDialogClose function shows alertDialog without callback function. 

confirmDialog
confirmDialog will show OK and Cancel buttons with message, and you can call functions when user clicks on OK and Cancel buttons.
Xrm.Utility.confirmDialog(message,yesCloseCallback,noCloseCallback)

Example:
contactScript =
{
    OnSaveContact: function () {
        Xrm.Utility.confirmDialog("Do you want to Create Followup Task?",
contactScript.CreateTask, contactScript.NoTaskCreated);
},
    NoTaskCreated: function () {
        Xrm.Utility.alertDialog("No Task created.", contactScript.TaskDialogClose);
    },
    TaskDialogClose: function () {
        Xrm.Utility.alertDialog("Dialog Closed");
    },
    TaskCreated: function () {
        Xrm.Utility.alertDialog("Task Created.");
    },
};

In this above example, when contact record is saved calling OnSaveContact function, which will ask user about creating follow-up task. This message will be shown using confirmDialog and based on user’s action either TaskCreated function will be called or TaskDialogClose function is called.

2 comments: