Dynamically Show/Hide Dynamics 365 Form Sections with JavaScript

In Microsoft Dynamics 365, forms can sometimes become overwhelming with numerous fields and sections. To streamline the user experience and guide users to the most relevant information, it's often necessary to display specific sections only when certain conditions are met. Dynamically showing and hiding sections based on field values is a powerful technique to achieve this.

Why Use Dynamic Section Visibility?

Implementing dynamic section visibility offers several key benefits:

Step-by-Step Guide to Implementing Dynamic Section Visibility

Let's walk through the process of creating dynamic form sections in Dynamics 365 using JavaScript.

Step 1: Identify the Controlling Field and Target Section

Before writing any code, you need to determine:

You can find these names by navigating to the form editor in Dynamics 365. Select the field or section, and its logical name will be displayed in the properties pane.

Step 2: Create a JavaScript Web Resource

Next, you'll create a JavaScript web resource to hold the logic for controlling section visibility. This involves writing a function that checks the value of your controlling field and then uses the Dynamics 365 client API to set the visibility of the target section.

Sample JavaScript Code

Here's a sample JavaScript function. Remember to replace placeholders with your specific field, tab, and section names.

function toggleSection(executionContext) {
    // Get the form context
    var formContext = executionContext.getFormContext();

    // Get the value of the controlling field (replace 'new_type' with your field's logical name)
    var controllingFieldValue = formContext.getAttribute('new_type').getValue();

    // Define the tab and section names (replace 'tab_general' and 'section_details')
    var targetTabName = 'tab_general';
    var targetSectionName = 'section_details';

    // Logic to determine visibility based on the field value
    // Example: Show section if 'new_type' is 100000000 (a specific option set value)
    if (controllingFieldValue === 100000000) {
        formContext.ui.tabs.get(targetTabName).sections.get(targetSectionName).setVisible(true);
    } else {
        formContext.ui.tabs.get(targetTabName).sections.get(targetSectionName).setVisible(false);
    }
}

Explanation:

Step 3: Register Events for the JavaScript Function

Once your JavaScript web resource is created and saved, you need to associate it with your form:

  1. Add the JavaScript Library to the Form: In the form editor, go to the 'Form Properties' and add your JavaScript web resource under the 'Events' tab.
  2. Register the 'OnLoad' Event: Within the 'Form Properties', under the 'Events' tab, select the 'OnLoad' event. Add your JavaScript function (e.g., toggleSection) as an event handler. Ensure the 'Pass execution context as first parameter' checkbox is ticked. This ensures the function runs when the form initially loads.
  3. Register the 'OnChange' Event for the Controlling Field: Select your controlling field (e.g., 'new_type') in the form editor. Open its properties, go to the 'Events' tab, and add your JavaScript function as an event handler for the 'OnChange' event. Again, ensure the execution context is passed. This makes the function run every time the value of the controlling field changes.

Testing Your Implementation

After saving and publishing your form customizations:

Best Practices

To ensure your dynamic forms are robust and maintainable:

Conclusion

Leveraging JavaScript to dynamically show or hide sections based on field values is a straightforward yet highly effective method for creating more intuitive and user-friendly forms in Dynamics 365. By reducing clutter and guiding users through relevant data, you can significantly enhance the overall user experience and improve data quality.