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
How It Works
Context Check:
The function first checks if theprimaryControl
(form context) is available. If it’s not, the function returns true to enable the button by default.Entity Verification:
The function retrieves the current record's entity name usingprimaryControl.data.entity.getEntityName()
. It compares this (after converting both values to lowercase) with the specifiedtargetTableName
. If they do not match, the function returns true (enabling the button).Form Verification:
Next, it checks if the form context supports the form selector (usingprimaryControl.ui.formSelector.getCurrentItem()
). If the current form's label matches the providedtargetFormName
(again using a case-insensitive comparison), the function returns false—disabling the button.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