Custom Rule in Ribbon Customization to enable/disable ribbon button in Dynamics CRM 2011

My requirement is to enable/disable Deactivate ribbon button on Account form based on following condition. In our case CRM gets accounts from other system, if account came from other system to CRM, user should not have capability to deactivate. We have a custom field “ls_cmid” on Account entity, based on this we want enable/disable Deactivate ribbon button. If this field “ls_cmid” value is NULL that means account is created in CRM, if it is not NULL, It came from other system.

Before going thru the steps, hoping you have basic understanding of the RibbonDiffXML.

Step1: Write following JavaScript methods in Account entity script.


//Disable Deactivate ribbon button If account came from CM
function DisableDeactivateButton() {
    var formType = Xrm.Page.ui.getFormType();
    if (formType != 1) {
        var isCMOwned = CheckIfAccountOwnedByCM();
        if (isCMOwned != null && isCMOwned) {
            return false;
        } else {
            return true;
        }
    }
    return true;
}
//Check if Account came from CM or not
function CheckIfAccountOwnedByCM() {
    if (Xrm.Page.getAttribute('ls_cmid') != null) {
        var cmId = Xrm.Page.getAttribute('ls_cmid').getValue();
        if (cmId != null && cmId.length > 0) {
            return true;
        }
    }
    return false;
}

Step2: Create a solution for Account entity and export into your local folder. Unzip the solution folder and open Customizations.XML file

Step3: In the CommandDefinition for Deactivate ribbon button you will see the EnableRules. You can have several rules and if any one of the rules returns False, the button will be disabled. Open Customizatons.XML file in Visual Studio and add new EnableRule  under <EnableRules> in <RuleDefinitions> file.


<EnableRule Id="LS.Mscrm.DisableDeactivateButton">
  <CustomRule FunctionName="DisableDeactivateButton" Library="$webresource:ls_script_account" Default="true" />
</EnableRule>

Step4: Update Deactivate CommandDefination section under <CommandDefinitions> in Customization.XML adding above rule. If you do not have this, you can find the CommandDefination for standard buttons on standard entities in the SDK folder.\sdk\resources\exportedribbonxml\ accountribbon.xml file.


<CommandDefinition Id="Mscrm.Form.Deactivate">
  <EnableRules>
    <EnableRule Id="Mscrm.CanWritePrimary" />
    <EnableRule Id="LS.Mscrm.DisableDeactivateButton" />
  </EnableRules>
  <DisplayRules>
    <DisplayRule Id="Mscrm.CanWritePrimary" />
    <DisplayRule Id="Mscrm.PrimaryIsActive" />
    <DisplayRule Id="Mscrm.PrimaryEntityHasStatecode" />
    <DisplayRule Id="Mscrm.PrimaryIsNotActivity" />
  </DisplayRules>
  <Actions>
    <JavaScriptFunction FunctionName="changeState" Library="/_static/_forms/form.js">
      <StringParameter Value="deactivate" />
      <CrmParameter Value="PrimaryEntityTypeCode" />
      <StringParameter Value="5" />
    </JavaScriptFunction>
  </Actions>
</CommandDefinition>

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s