Code Snippets

  • Hide/Show and Disable Components Based on Conditions
    JavaScript
    Hide/Show and Disable Components Based on Conditions or business logic requirements
This script enables you to dynamically customize the behavior of components on a Dynamics 365 form based on specific field values or predefined conditions. It allows you to hide, show, or disable fields and controls, ensuring that the user interface is responsive and tailored to the current context or business logic requirements.

function toggleComponentsBasedOnConditions(executionContext) { // Get the form context var formContext = executionContext.getFormContext(); // Example condition: Check if a field (e.g., statuscode) has a specific value var statusCode = formContext.getAttribute("statuscode").getValue(); // Replace with your field's logical name // Get controls to toggle (replace "fieldname" with your logical field names) var fieldToHide = formContext.getControl("fieldname1"); var fieldToDisable = formContext.getControl("fieldname2"); var fieldToShow = formContext.getControl("fieldname3"); // Apply conditions to hide/show/disable components if (statusCode === 1) { // Example: If status is "Active" fieldToHide.setVisible(false); // Hide field fieldToDisable.setDisabled(true); // Disable field fieldToShow.setVisible(true); // Show field } else if (statusCode === 2) { // Example: If status is "Inactive" fieldToHide.setVisible(true); // Show field fieldToDisable.setDisabled(false); // Enable field fieldToShow.setVisible(false); // Hide field } }