1

Disable column in Dynamics CRM editable subgrid based on a condition

I need to disable (make read-only) a column from an editable subgrid in a Dynamics CRM 365 form.

In MS doc (https://learn.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/mt788311(v=crm.8), the way to get this done is by getting controls with:

Xrm.Page.getControl("Opportunity_Installments").getGrid().getRows().getAll()[0].getData().entity.attributes.getAll()[0].controls

but the problem is that controls array is always null, so I cannot disable the column (apply setDisable function on control)

In IE console, the expression Xrm.Page.getControl("Opportunity_Installments").getGrid().getRows().getAll()[0].getData().entity.attributes.getAll()[0].controls returns null.

  • When do you want to disable Fields, OnLoad of Grid or on RecordSelect. I mean it is quite easy when user selects record then disable fields. This makes more sense w.r.t editable field. – AnkUser Aug 28 '19 at 09:31

1 Answers1

2

Most important thing: Xrm.Page is deprecated, you have to start using context.getFormContext().

Unfortunately the editable grid controls & internal things are not totally rendered on form load, we have to rely on OnRowSelectevent.

For performance reasons, a row (record) in an editable grid is not editable until the record is selected. Users must select a single record in a grid to edit it. Once a record is selected in an editable grid, Dynamics 365 internally evaluates a bunch of things including user access to the record, whether the record is active, and field validations to ensure that data security and validity are honored when you edit data. Consider using the OnRecordSelect event with the getFormContext method to access records in the grid that are in the editable state.

Reference

The workaround (available solution) is to use below snippet on OnRowSelect event.

function gridRowSelected(context) {
    context.getFormContext().getData().getEntity().attributes.forEach(function (attr) {
        if (attr.getName() === "new_fieldname") {
            attr.controls.forEach(function (c) {
                c.setDisabled(true);
            })
        }
    });
}

Read more