How to Disable Multiple Occurrences of the Same Field on a Dynamics 365 CRM Form Using JavaScript

Multiple Occurrences of the Same FieldMultiple Occurrences of the Same Field

In Microsoft Dynamics 365 CRM, a field can appear multiple times on a form, in different tabs, sections, or even within the same tab. However, when trying to manipulate such fields via JavaScript, you might notice that only one instance is affected while the others remain unchanged.

This happens because each occurrence of a field on a form is treated as a separate control, even though they all point to the same schema name in the database.

In this article, we will explore:

  • Why this happens
  • How CRM assigns control names
  • How to handle this scenario using JavaScript
  • An example to disable all instances of a field


Understanding the Issue

Let’s assume we have a custom field called “Customer Type” with the schema name new_customfield. This field appears in two different sections of the same form:

  1. General Information (Section 1)
    • Control Name: new_customfield
  2. Additional Information (Section 2)
    • Control Name: new_customfield_1

Even though both controls map to the same schema field (new_customfield), CRM treats them as separate UI elements with different control names.

Common Issue in JavaScript

If we try to disable the field using:

formContext.getControl("new_customfield").setDisabled(true);

This will only disable the first occurrence of the field, leaving the second instance (new_customfield_1) still editable.

Solution: Disabling All Instances of a Field

To ensure that all occurrences of the field are disabled, we need to loop through all controls linked to that field.

JavaScript Solution:

function disableAllInstancesOfField(executionContext) {
    var formContext = executionContext.getFormContext();
    var fieldName = "new_customfield";  // Schema name of the field

    // Retrieve all controls associated with this field
    var controls = formContext.getAttribute(fieldName).controls;

    // Loop through each control and disable it
    controls.forEach(function (control) {
        control.setDisabled(true);
    });
}

How This Works

  1. formContext.getAttribute(fieldName).controls
    • This retrieves all control instances of the field.
  2. Looping through controls
    • Each occurrence of the field is disabled individually.
  3. Ensures consistency
    • Regardless of how many times the field appears on the form, all instances will be disabled.


How to Implement This in CRM

  1. Create a JavaScript Web Resource
    • Navigate to Power Apps → Solutions.
    • Open your solution and go to Web Resources.
    • Click New and choose Script (JScript).
    • Upload the JavaScript file or add the function in a new script.

  2. Add JavaScript to Form Events
    • Open the Form Editor for the entity.
    • Click Form Properties.
    • Add the JavaScript Web Resource.
    • Attach the disableAllInstancesOfField function to the OnLoad event.
    • Click Save and Publish.


Targeting a Specific Instance

If you only want to disable one instance of the field (e.g., only in Section 2), use the control name directly:

function disableSpecificFieldInstance(executionContext) {
    var formContext = executionContext.getFormContext();

    // Disable only the control in "Additional Information" section
    formContext.getControl("new_customfield_1").setDisabled(true);
}

To find the control name:

  • Open the Form Editor in CRM.
  • Double-click the field where it’s placed.
  • Check the Control Name in the Details tab.


Key Takeaways:

Each instance of a field on the form is a separate control.
CRM assigns unique control names dynamically (e.g., new_customfield_1, new_customfield_2).
Using getControl() affects only one instance, not all.
To modify all occurrences, use getAttribute(fieldName).controls and loop through them.
You can target a specific instance using getControl(controlName).

By following this approach, you can ensure consistent field behavior across the form, preventing issues where only some instances are modified while others remain unchanged.

I hope this article helps! Let me know if you need further clarifications or enhancements. 😊

Leave a Reply

Your email address will not be published. Required fields are marked *