The most used JavaScript Methods for Dynamics CRM Part 3

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 the Data base value from Option Set Field

function GetOptionSetDataBaseValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get data base value of the option set field here, Give field logical name here
        var databaseValue = formContext.getAttribute(“new_gender”).getValue();
        Xrm.Utility.alertDialog(databaseValue);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Set the Date Field Value
function SetDateFieldValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the current Date
        var dateOfBirth = new Date();
        //Set the Current Date to date field
        formContext.getAttribute(“new_dateofbirth”).setValue(dateOfBirth);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Set the Lookup Field Value
function SetLookUpFieldValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        var lookupValue = new Array[1]();
        lookupValue[0] = new Object();
        lookupValue[0].id = “919F28C4-F9BB-E911-A977-000D3AF04F8C”; //Guid of the Record to be set
        lookupValue[0].name = “Tata Consultancy Services”; //Name of the record to be set
        lookupValue[0].entityType = “new_organization”; //Entity Logical Name
        formContext.getAttribute(“new_organizationid”).setValue(lookupValue);
    } catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Set the Field Requirement Level
function SetTheFieldRequirementLevel(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Set as Business Required
        formContext.getAttribute(“new_dateofbirth”).setRequiredLevel(“required”);
        //Set as Buiness Recommended
        formContext.getAttribute(“new_dateofbirth”).setRequiredLevel(“recommended”);
        //Set as Optional
        formContext.getAttribute(“new_dateofbirth”).setRequiredLevel(“none”);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Enable or Disable Field (Lock/Unlock)
function SetTheFieldState(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Set field as Read only
        formContext.getAttribute(“new_dateofbirth”).setDisabled(true);
        //Set field as Editable
        formContext.getAttribute(“new_dateofbirth”).setDisabled(false);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Apply Lookup Filter
function ApplyLookUpFilter(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the custom filter
        var filter = “”;
        //Get the lookup field
        var lookupField = formContext.getAttribute(“new_organizationid”);
        //Apply custom Filter for lookup
        lookupField.addPreSearch(function () {
            lookupField.addCustomFilter(filter);
        });
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Show Hide Fields
function ShowHideFields(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Show Field
        formContext.getAttribute(“new_dateofbirth”).setVisible(true);
        //Hide Field
        formContext.getAttribute(“new_dateofbirth”).setVisible(false);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}