Form and Grid Form Context

Form and Grid Form Context

Finally, after a long wait, editable grids are here. With the release of the editable grids, there have been some changes implemented not only on the grid, but also on the way that data that is manipulated from the form. There is now a single set of code that can be run from both the form and the grid, so that you don’t have to write your code separately for each.

Since we can’t use the Xrm.Page functions on the editable grid, we have to use the methods of the executionContext. The first thing that has to be done, is both on the editable grid field and on the form field, where we want to capture the event of the changed field, is pass the execution Context. This means our function header will look something like this:

function accountOnChange(context)

Within our function, we will need to get the entity and field data of the field that we are trying to capture. In our case, our field will be accountid. We first need to get the entity object. In order to get the entity object, we can use data.entity or getData().getEntity() function of getFormContext, as shown below:

var entityObject = context.getFormContext().data.entity;
// or
var entityObject = context.getFormContext().getData().getEntity();

Next we need to get the value of the attribute that we are looking for. We can use the getByName method to retrieve the attribute by Name, or we can loop through list of attributes as well.

var account = entityObject.attributes.getByName("accountid").getValue();

Finally, we can get the id and name values of the object that I just retrieved from the entityObject attribute by using the id and name properties:

var accountId = account[0].id;
var accountName = account[0].name;

That is basically it. We are using a single set of code to handle a field change event on both form and editable subgrid. Below is the full codeset:

function accountOnChange(context) 
{
var entityObject = context.getFormContext().getData().getEntity();
var account = entityObject.attributes.getByName("accountid").getValue();

var accountId = accountValue[0].id;
var accountName = accountValue[0].name;
}