getFormContext (Client API reference)

Applies to Dynamics 365 for Customer Engagement apps version 9.x

Returns a reference to the form or an item on the form depending on where the method was called.

Syntax

ExecutionContextObj.getFormContext()

Return value

Type: Object

Description: Returns a reference to the form or an item on the form such as editable grid depending on where the method was called. This method enables you to create common event handlers that can operate either on a form or an item on the form depending on where its called.

Example

The following sample code demonstrates how you can create a method that sets notification on a form field or editable grid cell depending on where you registered the script (Field OnChange event or editable grid OnChange event):

function commonEventHandler(executionContext) {
    var formContext = executionContext.getFormContext();    
    var telephoneAttr = formContext.data.entity.attributes.getByName('telephone1');
    var isNumberWithCountryCode = telephoneAttr.getValue().substring(0,1) === '+';

    // telephoneField will be a form control if invoked from a form OnChange event;
    // telephoneField will be a editable grid GridCell object if invoked from editable grid OnChange event.
    var telephoneField = telephoneAttr.controls.getByIndex(0);

    if (!isNumberWithCountryCode) {
        telephoneField.setNotification('Please include the country code beginning with ‘+’.', 'countryCodeNotification');
    }
    else {
        telephoneField.clearNotification('countryCodeNotification');
    }
}

Execution context

Form context