Sonoma Partners Microsoft CRM and Salesforce Blog

Extending the Dynamics 365 Editable Grid

Recently we blogged about the functionality of the new editable grid in the Dynamics 365 release.  One thing we pointed out in the post is that the editable grid doesn’t respect read-only fields on the form.  For example, if I have the Email field on the Contact form set to read-only and I use an editable grid for a Contact view that contains the Email field, it will allow the Email field to be editable.

One way to make the field read-only in the editable grid as well, is to write custom JavaScript.  In order to do so, we need to utilize the OnRecordSelect event of the editable grid.  First, create a JavaScript web resource with a function called onrowselect.  For the scenario of disabling the Email field on the Contacts sub-grid, the code will look like this:

function onrowselect(executionContext){
    var entityObject = executionContext.getFormContext().data.entity;
    entityObject.attributes.forEach(function (attribute, i) {
        if (attribute.getName() == "emailaddress1") {
            var emailControl = attribute.controls.get(0);
            emailControl.setDisabled(true);
            break;
        }
    }); 
}

The code uses the execution context to get a reference to the entity and then loops through each attribute to find the email attribute and then disables the control.

Now that we have the JavaScript web resource with the necessary function, we need to hook the function up to the editable grid.  Navigate to the form customizations where the editable grid is located.  Open the properties for the grid and there will be a new tab labeled “Events”.  Select “Events” and in the “Event” dropdown, choose “OnRecordSelect”.  Add your new JavaScript web resource and set the Function to “onrowselect” and be sure to check the option to pass in the execution context.

image

Click OK, save the form and publish customizations.  Now, navigate to your editable grid and when you select a row to edit it, the Email field should be disabled.

image

This is quick and easy to implement but the caveat here is that you need to apply this JavaScript to every editable grid for the desired entity, if it has a field that should be read-only.

Topics: Microsoft Dynamics 365 Microsoft Dynamics CRM