How to Get and Set Field Values in Dynamics 365 CRM using JavaScript

Customizing Dynamics 365 CRM using JavaScript allows you to dynamically retrieve and update field values, improving user experience and automation. This blog provides JavaScript examples for getting and setting different field types in Dynamics 365.

Getting Started

To begin, use the following function to get the form context:


function GetSet(executionContext) {
    var formContext = executionContext.getFormContext();
}

Now, let's explore different field types and how to work with them.

1. Single Line & Multiple Lines of Text Fields

Used for textual data entry.

Get Value:


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

Set Value:


formContext.getAttribute("xyz_name").setValue("John Doe");

2. Two Options (Boolean) Fields

Stores Yes/No or True/False values.

Get Value:


var interested = formContext.getAttribute("xyz_interested").getValue();

Set Value:


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

3. Option Set (Drop-Down) Fields

Used for predefined selections.

Get Selected Value (Numeric ID):


var topic = formContext.getAttribute("xyz_topic").getValue();

Get Selected Text:


var topicText = formContext.getAttribute("xyz_topic").getText();

Set Value Using ID:


formContext.getAttribute("xyz_topic").setValue(772500003);

Set Value Using Text:


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

4. Whole Number Fields

Used for storing integer values.

Get Value:


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

Set Value:


formContext.getAttribute("xyz_age").setValue(30);

5. Decimal & Floating Point Numbers

Used for precise numeric values.

Get Decimal Value:


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

Set Decimal Value:


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

Get Floating Point Value:


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

Set Floating Point Value:


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

6. Date Fields

Used to store date values.

Get Value:


var dateField = formContext.getAttribute("xyz_date").getValue();

Set Value (Current Date):


formContext.getAttribute("xyz_date").setValue(new Date());

7. Currency Fields

Stores financial values.

Get Value:


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

Set Value:


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

8. Multi-Select Option Set Fields

Allows selecting multiple predefined values.

Get Selected Values (Array of IDs):


var selectedValues = formContext.getAttribute("xyz_country").getValue();

Get Selected Texts:


var selectedTexts = formContext.getAttribute("xyz_country").getText();

Set Values Using IDs:


formContext.getAttribute("xyz_country").setValue([772500000, 772500002, 772500005]);

Set Values Using Texts:


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

9. Lookup and Customer Fields

Used to reference records like Contacts and Accounts.

Get Lookup Record Details:


var lookupValue = formContext.getAttribute("xyz_author").getValue();
var authorGuid = lookupValue ? lookupValue[0].id : null;
var authorEntityType = lookupValue ? lookupValue[0].entityType : null;
var authorName = lookupValue ? lookupValue[0].name : null;

Set Lookup Value:


var lookupRecord = [{
    id: "0d849e72-362b-eb11-a813-000d3af010d0",
    entityType: "contact",
    name: "John Doe"
}];
formContext.getAttribute("xyz_author").setValue(lookupRecord);

Conclusion

Understanding how to get and set field values in Dynamics 365 using JavaScript helps improve form automation and user experience. By leveraging these scripting techniques, you can customize CRM behavior efficiently.


FAQs

1. Can I update fields dynamically based on other field values?

Yes! Use the onChange event to trigger scripts when a field's value changes.

2. How do I reset a field to blank?


formContext.getAttribute("xyz_fieldname").setValue(null);

3. Where do I place this JavaScript code in Dynamics 365?

Create a Web Resource and link it to the form’s events (e.g., OnLoad, OnChange).

No comments:

Post a Comment