I want to show you how to hide/display a button on the command bar based on a field value on a related entity.
Here's how you do it!
As an example we have a custom entity called “Contact Role” and we want to hide the delete button on the command bar based on a field value on another entity called “Role”. Contact Role has a 1-to-many relationship with “Role”. On “Role” entity, there is a field called “Professional Only”. If this field’s value is equal to Yes, then we want to hide the delete button on Contact Role record, otherwise we display it:
1) First we need to use the REST service to get the value of the "Professional Only" field:
2) The next step is to use Ribbon Workbench. On Ribbon Workbench we need to add a display rule that can detect the current user’s permissions for a specific entity. Choose Contact Role entity on the Entities Navigation panel. Right click on Display Rules and add a “EntityPrivilegeRule” with “AppliesTo” set to PrimaryEntity, “PrivilegeDepth” set to Basic and “PrivilegeType” set to Delete
3) We also need to add several 'Enable Rules':
b) Add another Enable Rule and call it Mscrm.CanDeletePrimary. Right click on this rule and add the rule with the type “RecordPrivilegeRule”. On the right pane, set the “AppliesTo” to PrimaryEntity and “PrivilegeType” to Delete
c) Add the last Enable Rule and call it Mscrm.FormStateRule. Right click on this rule and add the type “FormStateRule”. On the right pane, set “InvertResult” to True and “State” to Create as we want to hide the delete button before we’re saving a record.
Now when you go back to the contact role entity if the "Professional Only" field is equal to Yes the delete button is hidden.