Requirments
I have a requirments to To disable all fields in a Dynamics CRM entity form based on a specific field value of its parent entity, you can use JavaScript. Below is an example of how to accomplish this.
Using the code
Below is a JavaScript example that disables all fields on a Contact entity form based on the customer type value of its parent Account entity. If the customer type is equal to 1, all Contact fields will be disabled.
Steps to Implement:
- Get the parent lookup (Account) ID
- Retrieve the account record using Xrm.WebApi
- Compare the field value
function applyDisableEnableAccordingParentStatus() {
var accountLookupItem = Xrm.Page.getAttribute("parentcustomerid");
if (accountLookupItem != null)
{
var accountlookupObjValue = accountLookupItem.getValue();
if (accountlookupObjValue != null)
{
var Id = accountlookupObjValue[0].id;
var Entity = "account";
var Select = "?$select=customertype";
if (Id != null)
{
Xrm.WebApi.retrieveRecord(Entity, Id, Select).then(
function success(result)
{
if (result != null)
{
if(result.customertype == 1 )
{
disableAllFormFields();
}
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
}
);
}
}
}
}
function disableAllFormFields()
{
Xrm.Page.ui.controls.forEach(function (control, i)
{
control.setDisabled(true);
});
}
References