Friday, 16 June 2017

{know-how} Editable grid disable fields conditionally using Javascript - Dynamics CRM D365 - D365_EditableGrid#1


Editable grid is an awesome way to manage related child records effectively and efficiently within few clicks and I personally love it for the same. For this kind of requirements, building a custom grid was the story for long time and now Editable grid is a real savior.

Before deep diving into code , below are the supported JavaScript events for editable grid. 

Scenario: In account form we have contact editable grid. Whenever the account name is DisableFields(felt lazy to find another field :P), disable all the fields in the editable grid row.

Event Registration:
  1. Open form editor and then open editable grid properties
  2. Navigate to Events tab and select Event named OnRecordSelect
  3. Add a function to the event.While adding function do not forget to enable "Pass execution context as first parameter"


Code Explanation:
To access the current row(entity record) we need the editable grid's formcontext which is a part of the context passed as first parameter. From there the rest of the code is similar to our traditional js implementations. 

function setFieldsDisabled(context) {
    var name = Xrm.Page.getAttribute("name").getValue();
    if (name == "DisableFields") {
        var contact = context.getFormContext().data.entity;
        contact.attributes.forEach(function(field, i) {
            field.controls.get(0).setDisabled(true);
        });
    }
}

Hope this helps! 

No comments:

Post a Comment

Contact Form

Name

Email *

Message *