The most used JavaScript Methods for Dynamics CRM Part 1

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.

//Execute all On Load events here

function OnLoad(executionContext) {

    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Sample code for On Load Event
        Xrm.Utility.alertDialog(“This is an alert for On Load Event.”);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
//Execute all OnSave events here
function OnSave(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Sample code for On Save Event
        Xrm.Utility.alertDialog(“This is an alert for On Save Event.”);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
//Execute Field on Change events here, This could be specific to each field
function OnChange(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Sample code for On Change Event
        Xrm.Utility.alertDialog(“This is an alert for On Change Event.”);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
//Get Lookup ID
function GetLookupId(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get lookup ID here, Give lookup field logical name here
        var lookupId = formContext.getAttribute(“new_organizationid”).getValue()[0].id;
        Xrm.Utility.alertDialog(lookupId);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
//Get Lookup Name
function GetLookupName(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get lookup name here, Give lookup field logical name here
        var lookupName = formContext.getAttribute(“new_organizationid”).getValue()[0].name;
        Xrm.Utility.alertDialog(lookupName);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
//Get the Entity Logical Name
function GetEntityLogicalName(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get entity logical name here, Give lookup field logical name here
        var entityName = formContext.getAttribute(“new_organizationid”).getValue()[0].entityType;
        Xrm.Utility.alertDialog(entityName);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
//Get the text value from Filed
function GetTextValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get text value of the field here, Give field logical name here
        var textValue = formContext.getAttribute(“new_employeebusinessid”).getValue();
        Xrm.Utility.alertDialog(textValue);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}