Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

To disable all fields in a Dynamics CRM entity form based on a specific field value of its parent entity,

0.00/5 (No votes)
7 Oct 2024CPOL 1.7K  
Javascript to disable all Dynamic CRM entity form fields according to their parent entity specific field value

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:

  1. Get the parent lookup (Account) ID
  2. Retrieve the account record using Xrm.WebApi
  3. Compare the field value
JavaScript
function applyDisableEnableAccordingParentStatus() {

    // Step 1: Get the parent lookup (Account) ID
    var accountLookupItem  = Xrm.Page.getAttribute("parentcustomerid");
	
	//Check contract Lookup Object is not null
    if (accountLookupItem != null)
	{		
		var accountlookupObjValue = accountLookupItem.getValue();
		//Check for Lookup Value
		if (accountlookupObjValue != null) 
		{
			var Id = accountlookupObjValue[0].id;
			var Entity = "account";
			var Select = "?$select=customertype";
		
			if (Id != null) 
			{
                // Step 2: Retrieve the account record using Xrm.WebApi
				Xrm.WebApi.retrieveRecord(Entity, Id, Select).then(
					function success(result) 
					{
						if (result != null) 
						{	
						    // Step 3: Compare the field value
							if(result.customertype == 1 )
							{
								disableAllFormFields(); 
							}
						}
					
					},
					function (error) {
						Xrm.Utility.alertDialog(error.message);
					}
				);
			}
		}
	}
}

 //Make all fields on the form read only (Disabled)
 function disableAllFormFields() 
 {
	Xrm.Page.ui.controls.forEach(function (control, i) 
	{
		control.setDisabled(true);
	});
 }

 

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)