Microsoft has provided an API facility for retrieving the data from the form directly using web-resource(JavaScript/HTML etc) which is known as Client API.
Client API helps us in many ways like:
- Get or set attribute values.
- Show and hide user interface elements.
- Reference multiple controls per attribute.
- Access multiple forms per entity.
- Manipulate form navigation items.
- Interact with the business process flow control.
Client API has 4 main objects:
- Execution Context
- Form Context
- Grid Context
- XRM object
I will be covering the two most used object in this blog which is Execution Context and Form Context.
Execution Context: executionContext defines the event (OnLoad, OnSave, OnChange) on which your code is executed. It passes the runtime information about the event which took place on the forms or grids like user information, form level information, attribute level information etc. It is used in the function as a object which gets all the necessary information from the CRM to our code . Therefore it is required to pass execution context as a first parameter inside the event handler.
function OnContactLoad(executionContext) { }
Form Context: formContext is a part of executionContext. It provides us an access to a form or form items using JavaScript. Before D365 v9.0 we were using XRM.Page which performed the same role as formContext. With the v9+ of CRM when XRM.Page is being deprecated hence it is always recommended to use formContext.
For accessing formContext below is the command:
var formContext = executionContext.getFormContext();
function OnContactLoad(executionContext)
{
var formContext = executionContext.getFormContext();
}
formContext contains two object:
Data object: This used to get the attributes data from the form.
Example: We have a text field on the form and we want to access the field data, then we will be using data object.
formContext.data.entity.attributes
or
formContext.getAttribute(“logicalname”).getValue();
UI object: This is useful when we want to hide any control or disable any control.
formContext.ui.controls
or
formContext.getControl(“logicalname”).getDisabled();
To know more about controls I would recommend to go through Microsoft documents on Controls.