locked
Enable or Disable a ribbon button based on a form value ??!! RRS feed

  • Question

  • Hi,

      I'm trying to disable a ribbon button on change of radio button value of  from "Opportunity " entity.  I have added a  Subgrid which connects to "Test " Entity.  so when i select that subgird " add new test" button will appear. I want this button to be disable/Enabled  based on the value in "Revenue[isrevenuesystemcalculated]" radio button. 

    This is something I tried : [importing fails after adding  this code]

                         

    <RibbonDiffXml>
            <CustomActions />
            <Templates>
              <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
            </Templates>
            <CommandDefinitions />
            <RuleDefinitions>
              <TabDisplayRules />
              <DisplayRules />
        <EnableRules>
                  <EnableRule id=" Mscrm.SubGrid.opportunity.AddNewRecord" />  
                  <ValueRule  Field="isrevenuesystemcalculated" Value="0" />
        </EnableRules>
     </RuleDefinitions>
            <LocLabels />

    </RibbonDiffXml>

    And this is the javascript to refresh the ribbon on change of radio button :

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

    PS : I've tried with different ribbon button ID. even  I experimented on a custom button still, I was not able to import the solution getting error, saying something wrong with the solution. :(




    • Edited by Pradeep47 Wednesday, May 9, 2012 8:39 AM
    Wednesday, May 9, 2012 8:14 AM

All replies

  • There seems to be a leading space in your EnableRule id.

    And you have to take note that the enable rule id must be the unique id and that is not to be the same as button id.

    If you're not familiar with customizing the ribbon, use CRM 2011 Visual Ribbon Editor tool to set those Enable/Disable rules and stuffs.

    If want to enable/disable out of the box buttons, it's not going to work with value rule. You need to control it with CustomActions and JavaScript.

    You can learn more about that in this post.


    By the way, you don't need to trigger refreshRibbon() for the subgrid buttons since those buttons are not loaded while you're changing the values.

    Wednesday, May 9, 2012 9:35 AM
  • Hi Pradeep,

    I'm assuming that you need to disable the 'Add Existing' ribbon button on a form sub-grid based on a value on the parent form. You can't use the ValueRule because this only looks at values on the selected entity (Test entity in your case), but you need to look at the Opportunity.isrevenenuesystemcalculated field.

    To get around this you need to use a 'Custom Rule' to call some javascript that uses the Xrm.Page functions to look at the value in the parent form.

    My example shows how to disable the ‘Add Existing’ button for the Competitor entity based on a value in the parent form. You will need to replace 'Competitor' with the entity that you need. I am using the Ribbon Workbench for CRM2011.


    1. Create a Javascript Webresource that contains the following script:

    function refreshRibbonOnChange()
    {
        Xrm.Page.ui.refreshRibbon();
    }
    
    function canAddCompetitor()
    {
     	var attribute = Xrm.Page.getAttribute("isrevenuesystemcalculated");
     	if (attribute!=null)
    {
    return (attribute.getValue()==true);
    }
    else
    {
    return true;
    }
    }

    2. Create a CRM Solution and add the Opportunity entity, the Competitor entity and the javascript webresource created above
    3. Open the Ribbon Workbench for CRM2011 add load your solution.
    4. Select the ‘Competitor’ entity and then select the ‘SubGrid’ ribbon in the top right drop down.
    5. Right click on the ‘Add Existing {0}’ button with the ID: Mscrm.SubGrid.opportunity.AddExistingAssoc
    This is the last button in the ‘Records’ group
     Select ‘Customise Command’ from the context menu on this button.
    6. In the ‘Solution Elements’ Panel, expand the ‘Commands’ and select the ‘Mscrm.AddExistingRecordFromSubGridAssociated’ command
    7. Right click and use ‘Edit Enable Rules’
    8. Click ‘Add New’ 
    9. Click ‘Add Step’ and select ‘Custom Rule’
       FunctionName: canAddCompetitor
       Library: Select the webresource you added to the solution.
    10. Publish your customisations from the Ribbon Workbench.
    11. From within CRM, Open the Opportunity Form Designer and add your webresource to the form 
    12. Double Click the Revenue Property and select the ‘Events’ tab
    13. Add a call to the RefreshRibbonOnChange function:
    14. Publish your customisations.

    The way it works is that when the sub-grid is loaded, the javascript function is called to see if the 'Add Existing' button should be enabled. This is done by customising the out of the box command to add the additional enable rule in:

    <CommandDefinition Id="Mscrm.AddExistingRecordFromSubGridAssociated">
          <EnableRules>
            <EnableRule Id="Mscrm.AppendToPrimary" />
            <EnableRule Id="Mscrm.EntityFormIsEnabled" />
            <EnableRule Id="new.competitor.CanAddCompetitor.EnableRule" />
          </EnableRules>
          <DisplayRules>
            <DisplayRule Id="Mscrm.AddExisting" />
            <DisplayRule Id="Mscrm.ShowForManyToManyGrids" />
            <DisplayRule Id="Mscrm.AppendToPrimary" />
            <DisplayRule Id="Mscrm.AppendSelected" />
          </DisplayRules>
          <Actions>
            <JavaScriptFunction FunctionName="Mscrm.GridRibbonActions.addExistingFromSubGridAssociated" Library="/_static/_common/scripts/RibbonActions.js">
              <CrmParameter Value="SelectedEntityTypeCode" />
              <CrmParameter Value="SelectedControl" />
            </JavaScriptFunction>
          </Actions>
        </CommandDefinition>

    The custom rule is named 'new.competitor.CanAddCompetitor.EnableRule' in our case, and looks like:

    <EnableRule Id="new.competitor.HasRevenue.EnableRule">
    <CustomRule FunctionName="canAddCompetitor" Library="$webresource:new_RefreshRibbon.js" />
    </EnableRule>

    Let me know how you get on.

    Hope this helps,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, May 9, 2012 12:56 PM
    Answerer