Home > Dynamics CRM 2011, Javascript, Ribbon, Xml > CRM 2011: Custom Enable Rule for SubGrid Ribbon Command

CRM 2011: Custom Enable Rule for SubGrid Ribbon Command

I came across a requirement where for an entity form with subgrid for a one-to-many relationship, the ‘Add New’ button should only be enabled when the statuscode is Draft.

The concept here is to add a new custom rule to the enable rules of the add new from subgrid command that calls the webresource’s javascript function that will check the value.

1. Using ExportRibbon tool, I exported the child entity ribbon. I found the command Mscrm.AddNewRecordFromSubGridStandard and copied the whole definition. This is the original CommandDefinition fresh from the export ribbon tool.

<CommandDefinition Id="Mscrm.AddNewRecordFromSubGridStandard">
	<EnableRules>
	  <EnableRule Id="Mscrm.AppendToPrimary" />
	  <EnableRule Id="Mscrm.EntityFormIsEnabled" />
	</EnableRules>
	<DisplayRules>
	  <DisplayRule Id="Mscrm.ShowForOneToManyGrids" />
	  <DisplayRule Id="Mscrm.AppendToPrimary" />
	  <DisplayRule Id="Mscrm.CreateSelectedEntityPermission" />
	  <DisplayRule Id="Mscrm.AppendSelected" />
	  <DisplayRule Id="Mscrm.HideAddNewForChildEntities" />
	</DisplayRules>
	<Actions>
	  <JavaScriptFunction FunctionName="Mscrm.GridRibbonActions.addNewFromSubGridStandard" Library="/_static/_common/scripts/RibbonActions.js">
		<CrmParameter Value="SelectedEntityTypeCode" />
		<CrmParameter Value="PrimaryEntityTypeCode" />
		<CrmParameter Value="FirstPrimaryItemId" />
		<CrmParameter Value="PrimaryControl" />
	  </JavaScriptFunction>
	</Actions>
</CommandDefinition>

2. I then added a new EnableRule in CommandDefinition\EnableRules

<EnableRule Id="Mscrm.AddNewRecordFromSubGridStandard.CustomRule"/>

3. I then defined the above EnableRule in RibbonDiffXml\RuleDefinitions

<RuleDefinitions>
  <TabDisplayRules>
  </TabDisplayRules>
  <DisplayRules />
  <EnableRules>
	<EnableRule Id="Mscrm.AddNewRecordFromSubGridStandard.CustomRule">
	  <CustomRule Library="$webresource:new_CustomRuleLibrary" FunctionName="FieldMustBeOfValue" Default="true">
		<StringParameter Value="statuscode" />
		<IntParameter Value="100000001" />
	  </CustomRule>
	</EnableRule>
  </EnableRules>
</RuleDefinitions>

4. I wrote a generic javascript function to check field value. This script is then added as a webresource named new_CustomRuleLibrary.

function FieldMustBeOfValue(fieldName, value) {
    var field = Xrm.Page.getAttribute(fieldName);
    if (field != null) {
        var fieldValue = field.getValue();
        return fieldValue == value;
    }
}

The RibbonDiffXml should look like this:

<RibbonDiffXml>
	<CustomActions />
	<Templates>
	  <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
	</Templates>
	<CommandDefinitions>
	  <CommandDefinition Id="Mscrm.AddNewRecordFromSubGridStandard">
		<EnableRules>
		  <EnableRule Id="Mscrm.AppendToPrimary" />
		  <EnableRule Id="Mscrm.EntityFormIsEnabled" />
		  <EnableRule Id="Mscrm.AddNewRecordFromSubGridStandard.CustomRule"/>
		</EnableRules>
		<DisplayRules>
		  <DisplayRule Id="Mscrm.ShowForOneToManyGrids" />
		  <DisplayRule Id="Mscrm.AppendToPrimary" />
		  <DisplayRule Id="Mscrm.CreateSelectedEntityPermission" />
		  <DisplayRule Id="Mscrm.AppendSelected" />
		  <DisplayRule Id="Mscrm.HideAddNewForChildEntities" />
		</DisplayRules>
		<Actions>
		  <JavaScriptFunction FunctionName="Mscrm.GridRibbonActions.addNewFromSubGridStandard" Library="/_static/_common/scripts/RibbonActions.js">
			<CrmParameter Value="SelectedEntityTypeCode" />
			<CrmParameter Value="PrimaryEntityTypeCode" />
			<CrmParameter Value="FirstPrimaryItemId" />
			<CrmParameter Value="PrimaryControl" />
		  </JavaScriptFunction>
		</Actions>
	  </CommandDefinition>
	</CommandDefinitions>
	<RuleDefinitions>
	  <TabDisplayRules>
	  </TabDisplayRules>
	  <DisplayRules />
	  <EnableRules>
		<EnableRule Id="Mscrm.AddNewRecordFromSubGridStandard.CustomRule">
		  <CustomRule Library="$webresource:new_CustomRuleLibrary" FunctionName="FieldMustBeOfValue" Default="true">
			<StringParameter Value="statuscode" />
			<IntParameter Value="100000001" />
		  </CustomRule>
		</EnableRule>
	  </EnableRules>
	</RuleDefinitions>
	<LocLabels />
</RibbonDiffXml>

Cheers – Sy

About these ads
  1. 15/11/2012 at 06:48

    Thanks for post this information, was very usefull for me.

    • 15/11/2012 at 11:39

      You’re very welcome Jessica.

  1. No trackbacks yet.

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

Follow

Get every new post delivered to your Inbox.

Join 112 other followers

%d bloggers like this: