Customizing Ribbon Buttons Based on Subgrid Context in Dynamics 365

 

Customizing Ribbon Buttons Based on Subgrid Context in Dynamics 365

Introduction

Customizing ribbon buttons in Microsoft Dynamics 365 can significantly enhance user experience, especially when differentiating actions based on specific subgrids. This article presents a scenario where we hide a ribbon button based on the subgrid's context using a custom JavaScript function. This method is particularly useful in scenarios with multiple subgrids of the same entity on a single form.

Scenario

In our scenario, we have two subgrids of the same entity on a form. We aim to conditionally display a custom button for each subgrid based on certain criteria, such as the subgrid's name, the entity, and the form where it's located.

Solution: hideSubgridButtonWithSubgridName Function

The core of our solution is the hideSubgridButtonWithSubgridName JavaScript function. This function checks the context of the subgrid and decides whether to display the button.


// ***************************************************************
// hideSubgridButtonWithSubgridName - Dynamically controls the visibility of ribbon buttons
// in a subgrid based on the subgrid's name, entity, and form.
//
// Parameters:
//   primaryControl: Reference to the form context.
//   selectedControl: Reference to the selected grid control.
//   entityName: Name of the entity.
//   formName: Name of the form.
//   subgridName: Logical name of the subgrid.
//
// Returns true if the button should be hidden; false otherwise.
// ***************************************************************
function hideSubgridButtonWithSubgridName(primaryControl, selectedControl, entityName, formName, subgridName)
{
    var formContext = primaryControl;
    if (!formContext || !selectedControl) {
        return false; // Essential controls are not available.
    }

    // Entity check
    var currentEntityName = formContext.data.entity.getEntityName();
    if (currentEntityName !== entityName) {
        return false;
    }

    // Form name check
    var currentFormName = formContext.ui.formSelector.getCurrentItem().getLabel();
    if (currentFormName !== formName) {
        return false;
    }

    // Subgrid name check
    var currentSubgridName = selectedControl.getName();
    if (currentSubgridName !== subgridName) {
        return false;
    }

    return true; // Conditions met, hide the button.
}


Implementation in Ribbon Workbench

  1. Create a Custom Command: In Ribbon Workbench, set up a custom command for the button.
  2. Define Custom Rule: Add a CustomRule that uses this JavaScript function.
  3. Configure Parameters: Ensure the ribbon button passes the necessary parameters (primaryControl, selectedControl, entityName, formName, subgridName) to the function.

Conclusion

This approach offers a dynamic and flexible way to control ribbon button visibility based on the subgrid context. It's a powerful way to enhance user interaction and ensure a more intuitive experience in Dynamics 365.

No comments:

Post a Comment