Skip to content

Enable/Disable a ribbon button dynamically based on a form value

Requirement:

You need to Enable or Disable a form ribbon button depending field values using the following rule:

Enable if:
(
    accountcategorycode=1
    OR
    accountcategorycode=2
    OR
    (
        accountcategorycode=3
        AND
        accountnumber != null
        AND
        accountnumber != ""
    )
)

Solution:

The CRM2011 Ribbon can read values from the current form from inside an Enable Rule. Using the Ribbon Workbench you can create an Enable Rule with a set of 'Or' rules. Each Or rule contains a one or more OrGroups. Inside the OrGroup, you can add Rules that all must be true in order that the OrGroup returns true.
So to implement the above, we must use:

OrEnableRule
Or
    OrGroup
        ValueRule (accountcategorycode=1)
    OrGroup
        ValueRule (accountcategorycode=2)
    OrGroup
        ValueRule (accountcategorycode=3)
        ValueRule (accountnumber='null', Invert=true)
        ValueRule (accountnumber='', Invert=true)

Notice that rather than using != (not equals) we are using a standard equals comparison, but selecting to invert the result, so if the rule returns true, it will be evaluated as false.

In the Ribbon Workbench, this would look like:

Or ValueRule

Be sure to associate your Enable Rule with your Command that is associated with your button.

Note: You will need to add a white space and delete it again to distinguish the Value of '' from a null value. To make a comparison with a null value, you provide the text 'null' for the value.

Once published, you'll notice that the button will only be enabled if the rules match the criteria when the form is loaded. To make the ribbon dynamically change when a field value changes, you'll need to create a JavaScript Webresource with the following JavaScript function:

function refreshRibbonOnChange()
{
    Xrm.Page.ui.refreshRibbon();
}

Within the onchange event of the accountcategorycode and accountnumber fields, call the refreshRibbonOnChange function:

On Change vent

Once published, you should see your ribbon button enabling and disabling based on your form values.

Note: Value rules cannot be used in Grid or HomePage ribbons. To implement similar functionality, you must create a javascript function to dynamically retrieve the data from the server and return true/false.

You can see a similar process as described here in the following video: http://www.develop1.net/public/post/Dynamically-ShowHide-a-standard-ribbon-button-based-on-a-form-value.aspx

Feedback and Knowledge Base