The most used JavaScript Methods for Dynamics CRM Part 6

Here’s a quick reference guide covering Microsoft Dynamics CRM syntax for common java script.

Most of the examples are provided as functions that you can easily test in the On Load and On Save by generating form Context from execution Context.

Get GUID of the Current Record

function GetGuidOfTheRecord(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the current Record Guid
        var recordGuid = formContext.data.entity.getId();
        Xrm.Utility.alertDialog(recordGuid);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get GUID of the Current User
function GetGuidOfTheRecord(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the Current User Guid
        var userGuid = formContext.context.getUserId();
        Xrm.Utility.alertDialog(userGuid);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get the Security Roles of Current User
function GetSecurityRolesOfCurrentUser(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the Current User Guid
        var userRoles = formContext.context.getUserRoles();
        Xrm.Utility.alertDialog(userRoles);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get the CRM Client Url
function GetTheCRMClientUrl(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the Client Url
        var clientUrl = formContext.context.getClientUrl();
        Xrm.Utility.alertDialog(clientUrl);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get the User Language ID
function GetTheUserLanguageID(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the Current User Language ID
        var userLanguage = formContext.context.getUserLcid();
        Xrm.Utility.alertDialog(userLanguage);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get the Current User Name
function GetTheCurrentUserName(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the Current User Name
        var userName = formContext.context.userSettings.userName;
        Xrm.Utility.alertDialog(userName);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Create Record using Xrm.WebApi
function CreateRecord() {
    try {
        var entityName = “new_organization”; //Entity Logical Name
        //Data used to Create record
        var data = {
            “new_organizationname”: “Tata Consultancy Services”,
            “new_description”: “This is the description of Tata Consultancy Services”,
            “new_noofemployees”: 400000,
            “new_revenue”: 20000000
        }
        Xrm.WebApi.createRecord(entityName, data).then(
            function success(result) {
                Xrm.Utility.alertDialog(“Success”);
            },
            function (error) {
                Xrm.Utility.alertDialog(“Error”);
            }
        );
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}