Disabling a Ribbon Button Based on the Current Table and Form in Dynamics 365

 In Dynamics 365, ribbon (or command bar) buttons can be enabled or disabled using enable rules. The enable rule functions should return true if the button should be enabled and false if the button should be disabled. In this example, we want to disable a button when the current record belongs to a specified entity (table) and is displayed on a specified form.

The function disableButtonFromSpecifiedTableForm checks the current record’s entity name and the current form’s label. If both match the target values (using a case-insensitive comparison), the function returns false (indicating the button should be disabled). Otherwise, it returns true.

The Function Code


/**
 * Determines if a button should be enabled based on the current record's entity and form name.
 * In Dynamics 365 ribbon enable rules, returning true enables the button,
 * while returning false disables it. This function returns false when the current
 * entity and form match the specified target values.
 *
 * @param {object} primaryControl - The form context of the current record.
 * @param {string} targetTableName - The logical name of the target entity.
 * @param {string} targetFormName - The display name of the form on which the button should be disabled.
 * @returns {boolean} True if the button should be enabled; false if it should be disabled.
 *
 * Usage:
 * // Disable the button when on the "Information" form of the "account" entity.
 * var enableButton = disableButtonFromSpecifiedTableForm(formContext, "account", "Information");
 * // In an enable rule, the button is enabled if the function returns true, and disabled if false.
 */
function disableButtonFromSpecifiedTableForm(primaryControl, targetTableName, targetFormName)
{
    if (primaryControl === null)
    {
        // Without context, default to enabling the button.
        return true;
    }

    // Retrieve the current entity's logical name.
    var currentEntityName = primaryControl.data.entity.getEntityName();
    if (currentEntityName.toLowerCase() !== targetTableName.toLowerCase())
    {
        // If not the target entity, enable the button.
        return true;
    }

    // Retrieve the current form's label.
    if (primaryControl.ui && primaryControl.ui.formSelector && typeof primaryControl.ui.formSelector.getCurrentItem === "function")
    {
        var currentFormItem = primaryControl.ui.formSelector.getCurrentItem();
        if (currentFormItem)
        {
            var currentFormLabel = currentFormItem.getLabel();
            // If the current form label matches the target form name (case-insensitive), disable the button.
            if (currentFormLabel.toLowerCase() === targetFormName.toLowerCase())
            {
                return false;
            }
        }
    }

    // Otherwise, enable the button.
    return true;
}

How It Works

  1. Context Check:
    The function first checks if the primaryControl (form context) is available. If it’s not, the function returns true to enable the button by default.

  2. Entity Verification:
    The function retrieves the current record's entity name using primaryControl.data.entity.getEntityName(). It compares this (after converting both values to lowercase) with the specified targetTableName. If they do not match, the function returns true (enabling the button).

  3. Form Verification:
    Next, it checks if the form context supports the form selector (using primaryControl.ui.formSelector.getCurrentItem()). If the current form's label matches the provided targetFormName (again using a case-insensitive comparison), the function returns false—disabling the button.

  4. Default Behavior:
    If none of the conditions for disabling are met, the function returns true, meaning the button remains enabled.

No comments:

Post a Comment