locked
CRM 2011 disabling custom ribbon button RRS feed

  • Question

  • Dear All,

    I need to disable one custom button in account entity ribbon once desired action is performed, please correct my following code if it contains any error.  

    <RibbonDiffXml>
      <CustomActions>
        <CustomAction Id="GT.account.Form.Star.CustomAction" Location="Mscrm.Form.account.MainTab.Workflow.Controls._children" Sequence="1">
          <CommandUIDefinition>
            <Button Command="GT.account.Form.Star.Command" CommandType="General" Id="GT.account.Form.Star.Button" Image16by16="$webresource:tci_submitbutton" Image32by32="$webresource:tci_submitbutton" LabelText="Submit to AX" TemplateAlias="o1" ToolTipDescription="Submit to AX" ToolTipTitle="Tip" />
          </CommandUIDefinition>
        </CustomAction>
      </CustomActions>
      <Templates>
        <RibbonTemplates Id="Mscrm.Templates" />
      </Templates>
      <CommandDefinitions>
        <CommandDefinition Id="GT.account.Form.Star.Command">
          <EnableRules />
          <DisplayRules />
          <Actions>
            <JavaScriptFunction Library="$webresource:tci_callingaxwebservice" FunctionName="SubmitToAx" />
          </Actions>
        </CommandDefinition>
      </CommandDefinitions>
      <RuleDefinitions>
        <TabDisplayRules />
        <DisplayRules>
          <DisplayRule Id="GT.account.Form.Star.Button">
            <FormStateRule Default="false" InvertResult="false" State="Disabled" />
          </DisplayRule>
        </DisplayRules>
        <EnableRules />
      </RuleDefinitions>
      <LocLabels />
    </RibbonDiffXml>


    Thanks & Regards, MS CRM Consultant, V.Surya.

    Thursday, March 22, 2012 7:46 AM
    Answerer

All replies

  • You will need to use an Enable Command with either a ValueRule or a CustomRule. With the value rule you will need a field (can be a checkbox) on the form indicating if the button should be available or not. With the CustomRule there is not need.

    These would be the two possibilities

          <EnableRule Id="Company.Form.account.MainTab.Save.test.Command.EnableRule.ValueRule">
            <ValueRule Field="fieldname" Value="value" />
          </EnableRule>
          <EnableRule Id="Company.Form.account.MainTab.Save.test.Command.EnableRule.CustomRule">
            <CustomRule Library="Webresource JS library" FunctionName="Function to Call" />
          </EnableRule>

    The function to call for the Custom Rule would be something like:

    function enableCheck() {
    return !(window._shouldEnableButton == true); }

    To your command function you would add:

    // When using the custom rule
    window._shouldEnabledButton = true;
    // When using the field
    Xrm.Page.getAttribute('fieldname').setValue(true);
    
    //Next for a reevaluation of all rules for the ribbon
    Xrm.Page.ui.refreshRibbon();

    The refreshRibbon call is required to force the ribbon to evaluate the display/enabled status of all buttons.

    If your own actions saves or refreshes the form this call is not necessary and the customrule approach as shown above will not work.


    Patrick Verbeeten
    www.patrickverbeeten.com
    www.wavextend.com

    Thursday, March 22, 2012 10:05 AM
  • Please correct if code is wrong

    function enableCheck()
    {
      return !(window._shouldEnableButton == true);
    }

    function enableCheck1()
    {

     
      return (window._shouldEnableButton == true);
       window._shouldEnabledButton = true;
       Xrm.Page.getAttribute('integrationstatus').setValue(true);
       Xrm.Page.ui.refreshRibbon();

    }


    Thanks & Regards, MS CRM Consultant, V.Surya.

    Monday, March 26, 2012 9:49 AM
    Answerer
  • Why do you have the statement return (window._shouldEnableButton== true); in the enableCheck1 (I asume this is the action function)

    If you want to exit the funciton if (window._shouldEnableButton== true) then you should use:

    if (window._shouldEnableButton== true)  { return; };

    The rest of it looks ok.

    If it does not work as expected try using the IE developers tools (press F12), this has a Javascript debugger you can use this to check what is happening. FYI the ribbon scripts are in the other scripts at the bottom of the list in one of the eval blocks, to find it start the debugger, click the ribbon button, use the script find function in the debugger and search for enableCheck, set a break point on all instances and click again.


    Patrick Verbeeten
    www.patrickverbeeten.com
    www.wavextend.com

    Monday, March 26, 2012 9:56 AM
  • Just Try this Customization. Your Solution will be resolved.

    <RibbonDiffXml>
      <CustomActions>
        <CustomAction Id="GT.account.Form.Star.CustomAction" Location="Mscrm.Form.account.MainTab.Workflow.Controls._children" Sequence="1">
          <CommandUIDefinition>
            <Button Command="GT.account.Form.Star.Command" CommandType="General" Id="GT.account.Form.Star.Button" Image16by16="$webresource:tci_submitbutton" Image32by32="$webresource:tci_submitbutton" LabelText="Submit to AX" TemplateAlias="o1" ToolTipDescription="Submit to AX" ToolTipTitle="Tip" />
          </CommandUIDefinition>
        </CustomAction>
      </CustomActions>
      <Templates>
        <RibbonTemplates Id="Mscrm.Templates" />
      </Templates>
      <CommandDefinitions>
        <CommandDefinition Id="GT.account.Form.Star.Command">
          <EnableRules />
          <DisplayRules />
          <Actions>
            <JavaScriptFunction Library="$webresource:tci_callingaxwebservice" FunctionName="SubmitToAx" />
          </Actions>
        </CommandDefinition>
      </CommandDefinitions>
      <RuleDefinitions>
        <TabDisplayRules />

    <DisplayRules>

                <DisplayRule Id="GT.account.Form.Star.Button.FormStateRule">

                  <FormStateRule State="Existing" />

                </DisplayRule>

                <DisplayRule Id="GT.account.Form.Star.Button.EntityPrivilegeRule">

                  <EntityPrivilegeRule EntityName="<Your Entity Name>" PrivilegeDepth="Basic" PrivilegeType="Write" />

                </DisplayRule>

              </DisplayRules>

              <EnableRules />

            </RuleDefinitions>

            <LocLabels>

              <LocLabel Id="GT.account.Form.Star.Button">

                <Titles>

                  <Title languagecode="1033" description="SubmitToAX" />

                </Titles>

              </LocLabel>

              <LocLabel Id="GT.account.Form.Star.Button.Description">

                <Titles>

                  <Title languagecode="1033" description="SubmitToAX Description" />

                </Titles>

              </LocLabel>

            </LocLabels>

          </RibbonDiffXml>

     



    Naren

    Wednesday, May 16, 2012 4:49 AM