Here’s how:

Step 1 – Create and register a Web Resource containing the required javascript

For my demo, I’ll add the functionality to the Quote form, so I’ve written the javascript as follows:

var _globalFormContext;

quoteScripts = {
    runOnLoad: function (context) {
        debugger;
        _globalFormContext = context.getFormContext();

        _globalFormContext.data.entity.attributes.forEach(function (a) {
            a.addOnChange(function () {
                _globalFormContext.ui.refreshRibbon();
            });
        });
    },

    formIsNotDirty: function() {
        var isNotDirty = true;
        var attributes = _globalFormContext.data.entity.attributes.get();
        if (attributes != null) {
            for (var i in attributes) {
                if (attributes[i].getIsDirty()) {
                    isNotDirty = false;
                    break;
                }
            }
        }
        return isNotDirty;
    }
};

Add the script as a Web Resource to your system:

Click “New”:

Enter a unique Name and a sensible Display Name and set the type to Script (JScript). Then click “Text Editor”:

Copy and paste the javascript code shown above and click “OK”.

Click “Save”, and when the saving is done, click “Publish”:

Step 2 – Add the runOnLoad function to the Form event handlers

Open the Main Form for the selected entity, in this case the Quote entity. Then click the Form Properties button and add the script you just added to the Form Libraries:

Then add the runOnLoad function to the event handlers executed on the Form’s OnLoad event, and be sure to check the “Pass execution context as first parameter” checkbox:

Click OK all the way back to the Form editor and save and publish your Form changes.

The purpose of the runOnLoad javascript is to register an onChange handler for all controls on the form. That handler is going to call an update on the Ribbon whenever a control is changed.

Step 3 – Add an EnableRule to the Entity Ribbon

Fire up the XRM Toolbox, load the Ribbon Workbench and open your entity in the Ribbon Workbench.

Add a new custom EnableRule to the Solution Elements of the entity:

Apply the new enable rule to the button command you wish it to apply to. If it is not a button/command you have added yourself, you will need to right click the relevant button and select “Customize Command” to get access to the button command.

In this example, I’ve added it to the “Create Order” button:

Voila, that is all. Publish all changes, both in the Ribbon Workbench and the CRM system, and test the new functionality!