Get & Set Field Values
in Dynamics 365
A complete JavaScript reference for all CRM field types
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.
function GetSet(executionContext) {
var formContext = executionContext.getFormContext();
}
01Single Line & Multiple Lines of Text
getValue() and setValue() API.
var name = formContext.getAttribute("xyz_name").getValue();
formContext.getAttribute("xyz_name").setValue("John Doe");
02Two Options (Boolean) Fields
getValue() returns a boolean; setValue() accepts true or false.
var interested = formContext.getAttribute("xyz_interested").getValue();
formContext.getAttribute("xyz_interested").setValue(true);
03Option Set (Dropdown) Fields
var topic = formContext.getAttribute("xyz_topic").getValue();
var topicText = formContext.getAttribute("xyz_topic").getText();
formContext.getAttribute("xyz_topic").setValue(772500003);
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
getValue() returns a number; setValue() accepts a whole number.
var age = formContext.getAttribute("xyz_age").getValue();
formContext.getAttribute("xyz_age").setValue(30);
05Decimal & Floating Point Numbers
var dn = formContext.getAttribute("xyz_dn").getValue();
formContext.getAttribute("xyz_dn").setValue(45.6);
var fpn = formContext.getAttribute("xyz_fpn").getValue();
formContext.getAttribute("xyz_fpn").setValue(0.00008);
06Date Fields
getValue() returns a JavaScript Date object; pass a Date object to setValue().
var dateField = formContext.getAttribute("xyz_date").getValue();
formContext.getAttribute("xyz_date").setValue(new Date());
07Currency Fields
var amount = formContext.getAttribute("xyz_amount").getValue();
formContext.getAttribute("xyz_amount").setValue(876.78);
08Multi-Select Option Set Fields
getValue() returns an array of numeric IDs; setValue() takes an array. Setting by text requires matching each label against getOptions().
var selectedValues = formContext.getAttribute("xyz_country").getValue();
var selectedTexts = formContext.getAttribute("xyz_country").getText();
formContext.getAttribute("xyz_country").setValue([772500000, 772500002, 772500005]);
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
getValue() returns an array with one object containing id, entityType, and name. setValue() takes the same structure.
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;
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
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.
null to setValue(). This works for all field types.
formContext.getAttribute("xyz_fieldname").setValue(null);
OnLoad or OnChange — through the form editor's event handler settings.