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.
[Step – 3] Save and publish your changes. Now you should see fields are locked when you select record on Editable grid.
Hope this helps!
its really helpful.
LikeLike
Appreciate your feedback!
LikeLike
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
LikeLike
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?
LikeLike
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.
LikeLike
shuld give schema name or wat
LikeLike
Thank you for reading my blog!
We should be giving Name not Schema name
LikeLike
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() + “.”);
};
});
};
LikeLike
Hi,Jeff.
Have you solved this problem? I’m facing the same situation, wondering if you could share any solution?
LikeLike