65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Oct 8, 2024

CPOL
viewsIcon

1740

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
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