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

Get & Set Field Values in Dynamics 365 | JavaScript Guide
Dynamics 365 · JavaScript

Get & Set Field Values
in Dynamics 365

A complete JavaScript reference for all CRM field types

JavaScript formContext Power Platform CRM Automation
Customizing Dynamics 365 CRM using JavaScript allows you to dynamically retrieve and update field values, improving user experience and automation. This guide covers getValue() and setValue() patterns for every field type you'll encounter in a CRM form.

Getting Started

Every JavaScript customization in Dynamics 365 starts with getting the form context from the execution context. All field operations flow through this object.

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

01Single Line & Multiple Lines of Text

📝 Used for textual data entry. Both single-line and multi-line text fields share the same getValue() and setValue() API.
Get Value
JavaScript
var name = formContext.getAttribute("xyz_name").getValue();
Set Value
JavaScript
formContext.getAttribute("xyz_name").setValue("John Doe");

02Two Options (Boolean) Fields

Stores Yes/No or True/False values. getValue() returns a boolean; setValue() accepts true or false.
Get Value
JavaScript
var interested = formContext.getAttribute("xyz_interested").getValue();
Set Value
JavaScript
formContext.getAttribute("xyz_interested").setValue(true);

03Option Set (Dropdown) Fields

📊 Used for predefined selections. You can get/set by numeric ID or by text label. Setting by text requires looping through the available options.
Get Selected Value (Numeric ID)
JavaScript
var topic = formContext.getAttribute("xyz_topic").getValue();
Get Selected Text
JavaScript
var topicText = formContext.getAttribute("xyz_topic").getText();
Set Value Using ID
JavaScript
formContext.getAttribute("xyz_topic").setValue(772500003);
Set Value Using Text
JavaScript
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);
    }
}

04Whole Number Fields

🔢 Used for storing integer values. getValue() returns a number; setValue() accepts a whole number.
Get Value
JavaScript
var age = formContext.getAttribute("xyz_age").getValue();
Set Value
JavaScript
formContext.getAttribute("xyz_age").setValue(30);

05Decimal & Floating Point Numbers

🔣 Used for precise numeric values. Decimal fields store fixed-precision numbers; floating point fields store approximate values with higher range.
Get Decimal Value
JavaScript
var dn = formContext.getAttribute("xyz_dn").getValue();
Set Decimal Value
JavaScript
formContext.getAttribute("xyz_dn").setValue(45.6);
Get Floating Point Value
JavaScript
var fpn = formContext.getAttribute("xyz_fpn").getValue();
Set Floating Point Value
JavaScript
formContext.getAttribute("xyz_fpn").setValue(0.00008);

06Date Fields

📅 Used to store date and date-time values. getValue() returns a JavaScript Date object; pass a Date object to setValue().
Get Value
JavaScript
var dateField = formContext.getAttribute("xyz_date").getValue();
Set Value (Current Date)
JavaScript
formContext.getAttribute("xyz_date").setValue(new Date());

07Currency Fields

💰 Stores financial values. Works identically to decimal fields — returns a number and accepts a number. The currency symbol is controlled by the record's currency setting.
Get Value
JavaScript
var amount = formContext.getAttribute("xyz_amount").getValue();
Set Value
JavaScript
formContext.getAttribute("xyz_amount").setValue(876.78);

08Multi-Select Option Set Fields

☑️ Allows selecting multiple predefined values. getValue() returns an array of numeric IDs; setValue() takes an array. Setting by text requires matching each label against getOptions().
Get Selected Values (Array of IDs)
JavaScript
var selectedValues = formContext.getAttribute("xyz_country").getValue();
Get Selected Texts
JavaScript
var selectedTexts = formContext.getAttribute("xyz_country").getText();
Set Values Using IDs
JavaScript
formContext.getAttribute("xyz_country").setValue([772500000, 772500002, 772500005]);
Set Values Using Texts
JavaScript
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);

09Lookup & Customer Fields

🔗 Used to reference records like Contacts and Accounts. getValue() returns an array with one object containing id, entityType, and name. setValue() takes the same structure.
Get Lookup Record Details
JavaScript
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
JavaScript
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 — from simple text fields to complex lookups and multi-select options.


FAQs

Q1 Can I update fields dynamically based on other field values?
Yes. Use the onChange event to trigger scripts when a field's value changes. Register your function on the field's OnChange event in the form editor, and it will fire every time the user modifies that field.
Q2 How do I reset a field to blank?
Pass null to setValue(). This works for all field types.
JavaScript
formContext.getAttribute("xyz_fieldname").setValue(null);
Q3 Where do I place this JavaScript code in Dynamics 365?
Create a Web Resource (type: JavaScript) in your solution, paste your function there, and then link it to the form's events — such as OnLoad or OnChange — through the form editor's event handler settings.

Tip: Always check that getValue() doesn't return null before reading properties from the result — especially for Lookup fields. A quick null check prevents the most common runtime errors in CRM scripts.

Dynamics 365 · JavaScript · Power Platform · CRM Customization Guide

No comments:

Post a Comment