Almost everything a form script does goes through one object: formContext. Every registered event handler receives it as part of the execution context, and it’s your entry point to reading and manipulating the form.

Getting the form context

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

This pattern — accepting executionContext as the first parameter and immediately pulling formContext from it — is the standard first line of nearly every modern form script function.

Key areas of the form context

  • formContext.getAttribute("fieldname") — access a specific column’s data: .getValue(), .setValue(), .setRequiredLevel(), .fireOnChange().
  • formContext.getControl("fieldname") — access the UI control itself: .setVisible(), .setDisabled(), .setNotification().
  • formContext.ui — form-level UI: the ribbon, tabs, form notifications, and the form’s current mode (create vs. update).
  • formContext.data — the record’s data as a whole: .save(), .refresh(), and .entity for entity-level metadata like the record’s ID and whether it’s dirty (unsaved changes).
Attribute vs. control — a distinction that trips people up constantly. The attribute is the data (the value itself, required level, business rules). The control is the UI representation of that data on this specific form (visible or not, enabled or not). A field can have a value (attribute) while its control is hidden — know which one you actually need to call before writing the line.

Legacy Xrm.Page

Older code and tutorials often use Xrm.Page instead of executionContext.getFormContext(). It still works in most cases but is deprecated — new code should always use the execution-context pattern, since Xrm.Page doesn’t reliably work in every context (like certain multi-form scenarios) and Microsoft has signaled it will eventually be removed.