By Ritchey Hazeu
Hi! In some scenario’s you would like to know if there are any related records and based on this condition, lock a form or any fields (or do any other action!).
First you would like to count the related records which are present in a subgrid:
var gridContext = formContext.getControl("Gridname");// get the grid context
var count = formContext.getControl("Gridname").getGrid().getTotalRecordCount();
And you would like to run this code after the Subgrid is loaded. So you will use the gridContext and let it run on load (apart from the form).
gridContext.addOnLoad(GridOnloadFunction);
How does this code look when you would like to lock fields?
function lockFieldsOnCount(executionContext) {
var formContext = executionContext.getFormContext(); // get the form context
var gridContext = formContext.getControl("Gridname");// get the grid context
var GridOnloadFunction = function () {
var count = formContext.getControl("Gridname").getGrid().getTotalRecordCount();
if (count > 0){
var lockAccount = formContext.getControl("field1").setDisabled(true);
var lockContact = formContext.getControl("field2").setDisabled(true);
}
else {
var lockAccount = formContext.getControl("field1").setDisabled(false);
var lockContact = formContext.getControl("field2").setDisabled(false);
}
};
gridContext.addOnLoad(GridOnloadFunction); //run onload of subgrid
}
How does this code look when you would like to lock a form?
function lockFieldsOnCount(executionContext) {
var formContext = executionContext.getFormContext(); // get the form context
var gridContext = formContext.getControl("Gridname");// get the grid context
var GridOnloadFunction = function () {
var count = formContext.getControl("Gridname").getGrid().getTotalRecordCount();
if (count > 0){
formContext.data.entity.attributes.forEach(function (attribute, index) {
var control = Xrm.Page.getControl(attribute.getName());
if (control) {
control.setDisabled(true)
}
});
else {
formContext.data.entity.attributes.forEach(function (attribute, index) {
var control = Xrm.Page.getControl(attribute.getName());
if (control) {
control.setDisabled(false)
}
});
}
};
gridContext.addOnLoad(GridOnloadFunction); //run onload of subgrid
}
Nice!