简体   繁体   中英

Disable column in Dynamics CRM editable subgrid based on a condition

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://docs.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.

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 OnRowSelect event.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM