Search This Blog

Wednesday, January 26, 2022

GET/SET field values using JavaScript in D365 CE

Introduction: In this blog, you’ll learn how to GET and SET field values of different data types in D365 CE(CRM) using JavaScript.

Single Line of Text and Multiple Lines of Text

GET

var name = formContext.getAttribute("vv_name").getValue();

Return Type is string

SET

formContext.getAttribute("vv_name").setValue("Vaishali");

NOTE: “vv_name” is the field Schema Name

Two Options

GET

var intrested = formContext.getAttribute("vv_interested").getValue();

Return Type is boolean

SET

formContext.getAttribute("vv_interested").setValue(true);

Note: Yes: true and No: false

Option Set

GET

To get option set value:
var topic = formContext.getAttribute("vv_topic").getValue();

To get option set Text:
var topicText= formContext.getAttribute("vv_topic").getText();

Return Type is Number for getValue() and String for getText()

SET

Set Option set using value
formContext.getAttribute("vv_topic").setValue(772500003);

Set Option set using Text

 var text = "Power Automate";
      var optionSetValues = formContext.getAttribute("vv_topic").getOptions();
      for (i = 0; i < optionSetValues.length; i++) {
           if (optionSetValues[i].text == text) {
            formContext.getAttribute("vv_topic").setValue(optionSetValues[i].value);
       }
    }

Whole Number

GET

var age = formContext.getAttribute("vv_age").getValue();

Return Type is Number

SET

formContext.getAttribute("vv_age").setValue(24);

Decimal Number

GET

var dn = formContext.getAttribute("vv_dn").getValue();

Return Type is Number

SET

formContext.getAttribute("vv_dn").setValue(45.6);

Floating Point Number

GET

var fpn = formContext.getAttribute("vv_fpn").getValue();

Return Type is Number

SET

formContext.getAttribute("vv_fpn").setValue(0.00008);

Date

GET

formContext.getAttribute("vv_date").getValue();

Return Type is Date

SET

formContext.getAttribute("vv_date").setValue(d);

Currency

GET

var amount = formContext.getAttribute("vv_amount").getValue();

Return Type is Number

SET

formContext.getAttribute("vv_amount").setValue(876.78);

Multi Select Option set

GET

To get Multi select option set values:
formContext.getAttribute("vv_country").getValue();

To get Multi select option set Text:
formContext.getAttribute("vv_country").getText();

Return Type is an array of numbers for getValue() and array of string for getText()

SET

Set Multi Select Option set using value
formContext.getAttribute("vv_country").setValue([772500000,772500002,772500005]);

Set Multi-Select Option set using Text

  var common=[];
        var optionText = ["India","Brazil","Canada"];
        var optionSetValues = formContext.getAttribute("vv_country").getOptions(); 
        for (i = 0; i < optionText.length; i++) {
            for (j = 0; j < optionSetValues.length; j++) {
                if (optionText[i] === optionSetValues[j].text) {
                    common.push(optionSetValues[j].value);
                    formContext.getAttribute("vv_country").setValue(common);
             }
         }
      }

Lookup and Customer

GET

var authorGuid = formContext.getAttribute("vv_author").getValue()[0].id;  
//to get the Guid of lookup record
var authorEntityType = formContext.getAttribute("vv_author").getValue()[0].entityType; 
// to get the entity name 
var authorName = formContext.getAttribute("vv_author").getValue()[0].name; 
// to get the name of the record

Return type is an array of lookup objects.

SET

 var authorLookup =new Array();
           authorLookup[0]= new Object();
           authorLookup[0].id = "0d849e72-362b-eb11-a813-000d3af010d0";  
           authorLookup[0].entityType = "contact";            
           authorLookup[0].name = "vaishali vyas";            

formContext.getAttribute("vv_author").setValue(authorLookup); 

No comments:

Post a Comment