none
How To Disable Ribbon Button Based On Status Reason

    Question

  • I need to disable a custom button on a ribbon based on the value of the Status Reason. Here's a more detailed overview:

    If the value of the Status Reason field is set to "Completed" (in either an active or inactive state) I want the button disabled, otherwise, it needs to be enabled.

    I don't know if there is a way to do this with JScript, or if it's something that needs to be coded into the rules section of the XML for the ribbon.

    Any help would be appreciated, and if anyone knows of a good learning resource for ribbon development I would be greatful

    Thanks

    Tuesday, April 12, 2011 2:39 AM

Answers

  • Use Enable Rule for a button:

     <EnableRule Id="buttonid">
                  <CustomRule Default="true" FunctionName="IsProductLineEqualsTerm" Library="$webresource:ipl_OpportunityButtonsEnableRules.js" />
                </EnableRule>

     

     

    function IsProductLineEqualsTerm()
    {

       var  productLine = Xrm.Page.data.entity.attributes.get('ipl_productline').getValue();

       var productLineName = (productLine != null ) ? productLine[0].name : "" ;
       
       productLineName  = productLineName.toLowerCase();

      return (( productLineName.indexOf( "term" , 0) >= 0));

    }

     

    Friday, May 20, 2011 5:00 PM

All replies

  • Hello,

    Please read this post: http://social.microsoft.com/Forums/en/crm/thread/ce3bc752-ebe6-4065-9848-2761f5a7c4ba.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Biz-Forward.comCroitoriu.NET

    Wednesday, April 13, 2011 8:07 AM
  • You can add customRule for your ribbon button, from custom rule you can call javascript function which return true or false.

    then you can disabled ribbon button

    See crm SDK to add custom rule

     


    Ketan Ghule
    Tuesday, April 19, 2011 11:55 AM
  • Hey,

    As mentioned above, your best bet to getting started on customizing the Ribbon is to read the documentation in the SDK. I'm not a programmer by trade, so it took me some time to mull over the documentation, but it will get you most of the way there. My contention with the Ribbon section of the SDK is that it lacks usable examples. Here are a few pointers that may help:

    1. First thing's first. You need to create a JScript Web Resource that will contain OnLoad code for the entity in question. This will be used later to be referenced in a CustomRule. The CustomRule Ribbon element calls this JScript function in the OnLoad event of your Entity. The function will either return a value of True or False, thus enabling the Ribbon Button in question, or disabling it. So all the function needs to do is get the StatusCode:
    2. function isStatusCompleted() {
        var returnValue = false;
        var formComplete = parseInt(Xrm.Page.data.entity.attributes.get("statuscode").getValue(), 10);
        if (formComplete == 2) {
          return returnValue;
        }
        else {
          return true;
        }
      }
      
      
    3. Then load this web resource into you entity and call the function from the OnLoad event. This is the easy part...
    4. If you haven't already done so, create a new solution and add every pertinent customization you've made so far (Using the Add Existing button). This includes all the Entities, Web Resources, SiteMap, that pertain to the entity in question for your Ribbon customization. And of course, include the Application Ribbons. The system is pretty good about automatically prompting you to add the necessary related components.
    5. Export the solution and save it to your Visual Studio Projects folder. I like to keep them in a folder called Solutions --> Exports. Unzip the files (Keep the zip file intact. Grab the Customization.xml file and open it in VS 2010, or create a project for it.
    6. When you open the XML file, you'll need to add some Schema files to the Properties window (in the Schema box) to get some Intellisense (which is extremely helpful for the non-programmer going off the SDK for the first time). These are found in the SDK download in the Schemas folder. Add all the ones that have "ribbon" in the file name.
    7. Do a search for <RibbonDiffXml>. This will take you down to the first Ribbon XML section of the customization file. You need to keep searching on this field until you come to the Entity in question. Now, here's the tricky part: The RibbonDiffXML section is at the bottom of the Entity's section. That is, when you find your entity, the element you jumped to in your search is actually at the bottom of the entity above the one in question. You'll want to jump to the next RibbonDiffXML element, which will be the one you want (at the bottom of your entity). I hope this makes sense, because it's important you find the right instance of the RibbonDiffXML element, or your solution import will fail.
    8. Now it's time to turn to the SDK. You will need to create an EnableRule that includes a CustomRule. The code will differ based on whether the button you need to enable/disable is a custom button, or an existing button. I can't give you all the details here, but here's some code from a similar bit of code that I created for my own EnableRule/CustomRule. Please keep in mind that this pertains to custom Ribbon buttons in a Custom Entity:
    9.     <RuleDefinitions>
           <TabDisplayRules />
           <DisplayRules>
            <DisplayRule Id="RibbonSolution.new_jobrequisition.form.FormStateNotNew.DisplayRule">
             <FormStateRule State="Create" InvertResult="true" />
            </DisplayRule>
            <DisplayRule Id="RibbonSolution.new_jobrequisition.WebClient.DisplayRule">
             <OrRule>
              <Or>
               <CrmClientTypeRule Type="Web" />
              </Or>
              <Or>
               <CrmClientTypeRule Type="Outlook" />
              </Or>
             </OrRule>
            </DisplayRule>
           </DisplayRules>
           <EnableRules>
            <EnableRule Id="RibbonSolution.new_jobrequisition.form.IsFormClonable.EnableRule">
             <CustomRule Default="false" FunctionName="IsClonable" Library="$webresource:new_JobRequisitionEntity.js" />
            </EnableRule>
            <EnableRule Id="RibbonSolution.new_jobrequisition.form.NotNew.EnableRule">
             <FormStateRule State="Create" InvertResult="true" />
            </EnableRule>
            <EnableRule Id="RibbonSolution.new_jobrequisition.WebClient.EnableRule">
             <OrRule>
              <Or>
               <CrmClientTypeRule Type="Web" />
              </Or>
              <Or>
               <CrmClientTypeRule Type="Outlook" />
              </Or>
             </OrRule>
            </EnableRule>
            <EnableRule Id="RibbonSolution.new_jobrequisition.form.IsTemplate.EnableRule">
             <CustomRule Default="true" FunctionName="isFormTemplate" Library="$webresource:new_JobRequisitionEntity.js" />
            </EnableRule>
            <EnableRule Id="RibbonSolution.new_jobrequisition.form.IsFormSubmitted.EnableRule">
             <CustomRule Default="true" FunctionName="isFormSubmitted" Library="$webresource:new_JobRequisitionEntity.js" />
            </EnableRule>
           </EnableRules>
          </RuleDefinitions>
      
      
    10. So once this is finished, you can save the Customization.xml file. Re-zip the entire contents of the original zip file and Import the solution. The system will tell you if it's Kosher or if it fails. If it fails, it won't finish the import and will give you somewhat handy details as to what you may have done wrong. Hopefully though, the above code can help guide you on how you code may look when complete... 

     


    --Dodd
    Tuesday, April 19, 2011 1:39 PM
  • I am doing similar thing to disable a button based on the picklist value of an attribute on the form.

    I am using enabled rule and a custom javascript function.

     

    I am experiencing some problem during form load that the button does not shows the right enabled or disabled state.

    I put some code in the onchange event of the attribute that controls the enable / disable state of the form and the onchange  do a ui.refreshRibbon and that works fine to refresh the ribbon.

    Does anyone know what else I need to do to get the button to display properly after onload ?

    Is this a MS bug?

    Thanks

     

    Wednesday, May 18, 2011 11:20 PM
  • Can you post your code?
    --Dodd
    Thursday, May 19, 2011 1:16 PM
  • Use Enable Rule for a button:

     <EnableRule Id="buttonid">
                  <CustomRule Default="true" FunctionName="IsProductLineEqualsTerm" Library="$webresource:ipl_OpportunityButtonsEnableRules.js" />
                </EnableRule>

     

     

    function IsProductLineEqualsTerm()
    {

       var  productLine = Xrm.Page.data.entity.attributes.get('ipl_productline').getValue();

       var productLineName = (productLine != null ) ? productLine[0].name : "" ;
       
       productLineName  = productLineName.toLowerCase();

      return (( productLineName.indexOf( "term" , 0) >= 0));

    }

     

    Friday, May 20, 2011 5:00 PM
  • There is a much easier way to do this, no code is required. All you need is a ValueRule definition. For example:

    <DisplayRule Id="custom.StatusReasonActive">
       <ValueRule Default="1" Field="statuscode" Value="1"/>
    </DisplayRule>

    • Proposed as answer by Caratacus Thursday, September 08, 2011 11:32 AM
    Thursday, September 08, 2011 11:32 AM
  • Hi,

    Here is a simple solution.

    First place the statuscode field on body of main form and uncheck visible by default from its properties(if you want to hide it).

    then simply add value rule for statuscode = 1 in enable or display rules ( where ever you want to use ) and you will get the desired result.

          <EnableRule Id="IBP.Form.new_entity.MainTab.Actions.button.Command.EnableRule.ValueRule">
            <ValueRule Field="statuscode" Value="1" Default="false" />
          </EnableRule>

    Hope it helps. :)

    • Proposed as answer by Harispk Tuesday, April 24, 2012 12:04 PM
    Tuesday, April 24, 2012 12:04 PM