The most used JavaScript Methods for Dynamics CRM Part 2

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);
    }
}
Get the Option Set text value
function GetOptionSetTextValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get text value of the option set field here, Give field logical name here
        var optionSetTextValue = formContext.getAttribute(“new_gender”).getText();
        Xrm.Utility.alertDialog(optionSetTextValue);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get the Data base value from Two Option Set Field
function GetTwoOptionSetDataBaseValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get the value of the two option set field here, Give field logical name here
        var databaseValue = formContext.getAttribute(“new_type”).getValue();
        Xrm.Utility.alertDialog(databaseValue);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get the Date Field Value
function GetDateFieldValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Getvalue of the field here, Give field logical name here
        var dateOfBirth = formContext.getAttribute(“new_dateofbirth”).getValue();
        Xrm.Utility.alertDialog(dateOfBirth);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Get the Date, Month, Year from Date Field Value
function GetDateFieldValues(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Get value of the field here, Give field logical name here
        var dateOfBirth = formContext.getAttribute(“new_dateofbirth”).getValue();
        //Get Year
        Xrm.Utility.alertDialog(dateOfBirth.getFullYear());
        //Get Month
        Xrm.Utility.alertDialog(dateOfBirth.getMonth());
        //Get Date(Day)
        Xrm.Utility.alertDialog(dateOfBirth.getDate());
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Set the text value to Text Field
function SetTextFieldValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Set the field value here
        formContext.getAttribute(“new_employeebusinessid”).setValue(“abcd”);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}
Set the Data base value to Option Set Field
function SetOptionSetDataBaseValue(executionContext) {
    try {
        //Get the form context
        var formContext = executionContext.getFormContext();
        //Set the field value here
        formContext.getAttribute(“new_gender”).setValue(123456);
    }
    catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}