Pages

Monday, 12 November 2018

Get or Update fields on form using JavaScript in Dynamics 365 v9


After Microsoft released v9.0 of Microsoft Dynamics 365, Xrm.page became deprecated that it have been replaced by ExecutionContext.getFormContext(). Existing JavaScript code that uses Xrm.Page will still work after v9.0 but will be totally removed/unsupported after a few more update.

So in this blog post I am going to use execution context to get or set fields values in CRM record.
I will work for following field types:
      1.       Single Line / Multiple Line of Text
      2.       Two Options 
      3.       Option Set 
      4.       Multi-Select Option Set 
      5.       Whole Number 
      6.       Decimal Number 
      7.       Floating-Point Number 
      8.       Currency 
      9.       Date and Time 
      10.   Lookup / Customer
Below is the script that will describe the code:

function WorkingWithFields(executionContext) {
    //Single line of text
    var SingleLineText = executionContext.getFormContext().getAttribute("name");
    if (SingleLineText != null)
        console.log("current single line of text value is:" + SingleLineText.getValue());
    SingleLineText.setValue("Test Jscript");

    //Whole Number
    var wholeNumber = executionContext.getFormContext().getAttribute("numberofemployees");
    if (wholeNumber != null)
        console.log("current whole number value is:" + wholeNumber.getValue());
    wholeNumber.setValue(123);

 
  //decimal number
    var decimalNumber = executionContext.getFormContext().getAttribute("exchangerate");
    if (decimalNumber != null)
        console.log("current decimal number value is:" + decimalNumber.getValue());
    decimalNumber.setValue(123.12);
 
  //floating point number
    var floatNumber = executionContext.getFormContext().getAttribute("address1_latitude");
    if (floatNumber != null)
        console.log("current float number value is:" + floatNumber.getValue());
    floatNumber.setValue(-50.9934534);

    //two options
    var twoOptions = executionContext.getFormContext().getAttribute("donotemail");
    if (twoOptions != null)
        console.log("current two Options value is:" + twoOptions.getValue());
    twoOptions.setValue(true);

    //lookup
    var lookupField = executionContext.getFormContext().getAttribute("parentaccountid");
    if (lookupField != null)
        console.log("current lookup Field value is:" + lookupField.getValue());
    // To get the Id, Name and Entity Name (account/contact)
    var reocord_id = lookupField.getValue()[0].id;
    var record_name = lookupField.getValue()[0].name;
    var record_entityName = lookupField.getValue()[0].entityType;

    // Set its field value
    lookupField.setValue([{
        id: reocord_id,
        name: record_name,
        entityType: record_entityName
    }]);
   
    //multiselect
    var multiSelectField = executionContext.getFormContext().getAttribute("new_multiselect_field");
    if (multiSelectField != null)
        console.log("current multi Select Field value is:" + multiSelectField.getValue());
    multiSelectField.setValue([1,2,3]);

    //optionset
    var optionSetField = executionContext.getFormContext().getAttribute("customersizecode");
    if (optionSetField != null)
        console.log("current option Set Field value is:" + optionSetField.getValue());
    optionSetField.setValue(3);

    //Currency
    var CurrencyField = executionContext.getFormContext().getAttribute("creditlimit");
    if (CurrencyField != null)
        console.log("current Currency Field value is:" + CurrencyField.getValue());
    CurrencyField.setValue(145000034);

    //Date and time
    var DateTimeField = executionContext.getFormContext().getAttribute("lastonholdtime");
    if (DateTimeField != null)

        console.log("current Date Time Field value is:" + DateTimeField.getValue());
    DateTimeField.setValue(new Date());
}

Hope you will find this blog helpful.

Happy CRMing ðŸ˜Š

Regards,
Preeti Sharma

4 comments:

Get or Update fields on form using JavaScript in Dynamics 365 v9

After Microsoft released v9.0 of Microsoft Dynamics 365, Xrm.page became deprecated that it have been replaced by ExecutionContext.getFor...