Dynamics CRM Javascript · Javascript

{Dynamics CRM 365} Disable fields in Editable Grid View using Javascript

Sometimes we get the requirement from our client to disable fields based on some conditions on Editable grid views. To disable fields on Editable Grid views, We can write javascript code on OnRecordSelect event handler of Editable Grid.

[Step – 1] Create new web resource and add below Javascript code.

function onRecordSelect(exeContext) {
//debugger;
var _formContext = exeContext.getFormContext();
var disableFields = ["name","estimatedclosedate"];
lockFields(exeContext, disableFields);
}

function lockFields(exeContext, disableFields) {
var _formContext = exeContext.getFormContext();
var currentEntity = _formContext.data.entity;
currentEntity.attributes.forEach(function (attribute, i) {
if (disableFields.indexOf(attribute.getName()) > -1) {
var attributeToDisable = attribute.controls.get(0);
attributeToDisable.setDisabled(true);
}
});
}

[Step – 2] Now navigate to Entity->Opportunity->Events->Add Web resource library->Add onrecordselect envent handler and attach method. Do not forget to select pass execution context as first parameter.

EditableGrid

[Step – 3] Save and publish your changes. Now you should see fields are locked when you select record on Editable grid.

EditableGrid01

Hope this helps!

Advertisement

10 thoughts on “{Dynamics CRM 365} Disable fields in Editable Grid View using Javascript

  1. Hi,

    On my experience, this method is not working for the new Unified Interface. Is there a way to lock the fields of an editable grid using javascript in the Unified Interface?

    Thank you

    Like

    1. Hello Eduardo,

      Thanks for reading my blog!

      I have tested this code on my CRM instance which is Dynamics 365 UCI version 9.1 and it seems to be working fine.

      Could you please share your source code to debug your issue?

      Like

  2. Hello and thanks for this solution,

    does it work for more than one editable subgrids in the same form? I have 3 different editable subgrids (3 different entities) in the Opportunity main form (unified interface) and it doesn’t seem to work for more than one subgrid at the same time.

    Thank you.

    Like

  3. I have a requirement that the list of columns to lock is different based on conditions pertaining to the record and the user. Getting that information requires using functions that return a promise but this prevents the lockFields() part of the code from actually locking the fields. There is no error message and when using console.log() it reports that the field is actually locked but the user can still edit the field in the UI. Any idea why that would be?

    A sample of the code is below.

    function onRecordSelect(exeContext) {
    var _formContext = exeContext.getFormContext();
    var disableFields = new Array();
    getCurrentUserDetails.then(
    function (currentUser) {
    if (currentUser.isAllowedToEdit == true) {
    disableFields = [“fieldOne”, “fieldTwo”, “fieldThree”];
    }
    else {
    disableFields = [“fieldOne”, “fieldThree”, “fieldFour”, “fieldFive”, “fieldSix”];
    };
    lockFields(exeContext, disableFields)
    },
    function (error) {
    console.log(error.meessage);
    disableFields = [“fieldOne”, “fieldTwo”, “fieldThree”, “fieldFour”, “fieldFive”, “fieldSix”];
    lockFields(exeContext, disableFields)
    }
    );
    };

    function lockFields(exeContext, disableFields) {
    var _formContext = exeContext.getFormContext();
    var currentEntity = _formContext.data.entity;
    currentEntity.attributes.forEach(function (attribute, i) {
    if (disableFields.indexOf(attribute.getName()) > -1) {
    var attributeToDisable = attribute.controls.get(0);
    attributeToDisable.setDisabled(true);
    //The console log below returns true for each expected field but the field is still editable in the UI.
    console.log(attributeToDisable._controlName + ” disabled, ” + attributeToDisable.getDisabled() + “.”);
    };
    });
    };

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s