Alert Dialog and Confirm Dialog in MSCRM 2013 Using Javascript

Especially Alert and Confirm Dialog designed for Mobiles/Tablets replacing window.alert and window.confirm.

It’s a best practice to use the new Syntax for Online/On premise as well.

Alert Dialog: Displays a dialog box like alert.

Syntax:

Xrm.Utility.alertDialog(message,onCloseCallback)
Parameter Name Type Description
message String The text of the message to display in the dialog.
onCloseCallback Function A function to execute when the OK button or Close button clicked.Use null if you don’t want callback.

Example: Set Personal Notes field with some text in Contact Entity Onload after Alert Click on Ok or Cancel/Close

Solution: Copy and paste the below Code in Contact Entity and call showAlertDialog on onLoad

function showAlertDialog() {
    Xrm.Utility.alertDialog("Click Ok or Cancel to set the Personal Notes Value", function() {
        Xrm.Page.getAttribute("description").setValue("alertDialog Fired");
    });
}

Output:

showAlertDialog
After Click on Ok or Cancel,

showAlertDialog Fired

Confirm Dialog: Displays a confirmation dialog box that contains an optional message as well as OK and Cancel buttons.

Syntax: 

Xrm.Utility.confirmDialog(message,yesCloseCallback,noCloseCallback)
Parameter Name Type Description
message String The text of the message to display in the dialog.
yesCloseCallback Function A function to execute when the OK button is clicked.Use null if you don’t want callback.
noCloseCallback Function A function to execute when the Cancel button is clicked.Use null if you don’t want callback.

Example: Set Personal Notes field with some text in Contact entity Onload after Confirm Click on Ok or Cancel/Close

Solution: Copy and paste the below Code in Contact Entity and call showConfirmDialog on onLoad

function showConfirmDialog() {
    Xrm.Utility.confirmDialog("Click Yes or No to set the Personal Notes Value",
        function() {
            Xrm.Page.getAttribute("description").setValue("Yes Callback fired");
        },
        function() {
            Xrm.Page.getAttribute("description").setValue("No Callback fired");
        });
}

Output:

showConfirmDialog
Click on Ok to see the below result,

showConfirmDialog Yes

Click on Cancel or Close to see the below result,

showConfirmDialog No

Please provide your valuable comments on this article.

Notifications in MSCRM using Javascript

It’s a new JavaScript feature in CRM 2013.

setFormNotification

 To Display Form Level Notifications we can use this method. The height of the Notification area is limited. So, every new notification will be displayed on the top. Users can scroll down for the older notifications.

Syntax:

Xrm.Page.ui.setFormNotification(message, level, uniqueId);
Parameter Name Type Description
message String The text of the message
level String The level defines how the message will be displayed.
ERROR : Notification will use the system error icon.
WARNING : Notification will use the system warning icon.
INFO : Notification will use the system info icon.
uniqueId String A unique identifier for the message used with clearFormNotification to remove the notification.

Example: Display all Form Level Notifications on OnLoad of Case entity.

Solution:Copy & Paste the below functions in a JScript Webresource and add the function callFormNotification on Onload

function callFormNotification() {
 setFormNotification("Error Notification", "ERROR", "1");
 setFormNotification("Warning Notification", "WARNING", "2");
 setFormNotification("Information Notification", "INFO", "3");
}
function setFormNotification(msg, msgType, uniqueId) {
 Xrm.Page.ui.setFormNotification(msg, msgType, uniqueId);
}

Output:

Notification Output

clearFormNotification

To remove Form Level Notifications we can use this method.

Syntax:

Xrm.Page.ui.clearFormNotification(uniqueId)
Parameter Name Type Description
uniqueId String A unique identifier for the message used with setFormNotification to set the notification.

Example: Remove error Notification in the above example.

Solution: Copy & Paste the below function in a JScript Webresource and add the function removeErrorNotification on Onload

function removeErrorNotification() {
 clearFormNotification("1");
}
function clearFormNotification(uniqueId) {
 Xrm.Page.ui.clearFormNotification(uniqueId);
}

Output:

clearNotificationOutput

Field Level Notifications:

setNotificaion: To Set Notification at field Level.
Sytax:

Xrm.Page.getControl(fieldSchemaName).setNotification(message);
Parameter Name Type Description
fieldSchemaName String Provide field Schema Name
message String Provide message to display

clearNotification: To Remove Notificaion at field Level

Sytax:

Xrm.Page.getControl(fieldSchemaName).clearNotification();

Please provide your valuable comments on this article.

Show/Hide Navigation Using JScript in CRM

Name Type Description
navItemSchemaName String Provide Navigation Schema name (starts with nav keyword).
Example: navAudit, navDocument etc
VisibleType String Provide Yes (true) or No (false) to Show or Hide Navigation
function showHideNavigation(navItemSchemaName, VisibleType) {
    var objNavItem = Xrm.Page.ui.navigation.items.get(navItemSchemaName);
    if (objNavItem != null) {
        if (VisibleType == "No")
            objNavItem.setVisible(false);
        else if (VisibleType == "Yes")
            objNavItem.setVisible(true);
    }
}

Example:Hide Audit History Navigation in Lead

Solution:Follow the below Steps,
Click on F12 to open Developer Tools and find for the id of Audit History

Hide Audit History

Include the below Code in Javascript Webresource and call in Onload.

function hideAuditHistoryNav() {
    showHideNavigation("navAudit", "No");
}

function showHideNavigation(navItemSchemaName, VisibleType) {
    var objNavItem = Xrm.Page.ui.navigation.items.get(navItemSchemaName);
    if (objNavItem != null) {
        if (VisibleType == "No")
            objNavItem.setVisible(false);
        else if (VisibleType == "Yes")
            objNavItem.setVisible(true);
    }
}

Output:

Please provide your valuable comments on this article.

Set Lookup Value using JavaScript in MSCRM

Syntax:

Name Type Description
lookUpSchemaName String The lookup attribute logical name
entitySchemaName String The logical name of the entity being set.
recordId String A string representation of the GUID value for the record being set.
The expected format is “{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}”.
recordName String The text to be displayed in the lookup.
function setLookupValue(lookUpSchemaName, entitySchemaName, recordId, recordName) {
    var lookUpObj = [];
    lookUpObj[0] = {};
    lookUpObj[0].id = recordId;
    lookUpObj[0].entityType = entitySchemaName;
    lookUpObj[0].name = recordName;
    if (Xrm.Page.getAttribute(lookUpSchemaName) != null)
        Xrm.Page.getAttribute(lookUpSchemaName).setValue(lookUpObj);
}

Alternate Method

function setLookupValue(lookUpSchemaName, entitySchemaName, recordId, recordName) {
    Xrm.Page.getAttribute(lookUpSchemaName).setValue([{
        entityType: entitySchemaName,
        id: recordId,
        name: recordName
    }]);
}

Please provide your valuable comments on this article.