Hello everyone,
This is my first blog post on Dynamics CRM 2011. I have been going through Dynamics CRM 2011 SDK and it has fantastic stuff to learn. I went through the
Walkthrough: Add a Custom Button to an Existing Group for a Specific Entity Ribbon customization stuff in the SDK and able to do it. In that walkthrough, by using
“CrmClientTypeRule” and
“FormStateRule” rules custom button on the Form(not HomePageGrid) has been enabled only for “Web Client” and for “Existing Records”. This is the result of SDK sample:
Now, In this post I would like to add few more things on how to use “ValueRule” and “OrRule” rules with “EnableRules”.
Lets consider, if we want to enable the Custom button only when a specific filed on the form has a value. In that scenario, we can use “ValueRule” which can retrieve a specific field value on the form. Assume that we have to show Custom button only when “Fax” filed has value. In that case we can think of two approaches. One is, if “Fax” field is null then disable the Custom Button and the other one is, if “Fax” field has “anyvalue” then “Enable” the custom button. However, for ribbon customizations we have only “EnableRule”. And certainly “EnableRule” only works with “Equality” condition. So, In order to support different conditions we have one useful attribute called “InvertResult” which negates the result(Instead of looking for “not equal” condition, go with equal condition and use “InvertResult”).
Following the piece of code we need to place it under //RibbonDiffXml/RuleDefinitions/EnableRules/EnableRule/
<EnableRule Id=”Sample.account.form.CheckFaxValue.EnableRule”>
<ValueRule Field=”fax” Value=”null” InvertResult=”true”/>
</EnableRule>
Here I have used unique Id for the “EnableRule” which can be used to refer in the “CommandDefinition”. For the “ValueRule”, I have defined three attributes. “Field” refers to the schema name of particular field on the form from which we are trying to retrieve a value. “Value” attribute will hold user given value which will be checked against “Field” attribute’s value for equality. In our case, I decided to go with “null” check condition and “InvertResult”. So, I have given “null” as the value and for the third attribute “InvertResult”, I have given “true” as the value. Whenever “Fax” filed has value then the returned value from “ValueRule” is “True”(as we are using Invert Result) and “False” if it is “null”.
After placing above code, refer the id in the following location //RibbonDiffXml/CommandDefinitions/CommandDefinition/EnableRules as
<EnableRule Id=”Sample.account.form.CheckFaxValue.EnableRule”/>
Now, export the solution and publish it. Below will be the result when Fax doesn’t have any value:
And when “Fax” field has a value:
That’s cool. With single line additional code we can enable/disable custom button based on field value.
Now, I thought of checking with “Option Set” fields and want to whether it considers “Selected Value” or “Selected Text”. Certainly, it is “Selected Value” by default. Below is the piece of code I wrote:
<EnableRule Id=”Sample.account.form.CheckContactValue.EnableRule”>
<ValueRule Field=”address1_addresstypecode” Value=”1″/>
</EnableRule>
<EnableRule Id=”Sample.account.form.CheckFaxValue.EnableRule”>
<ValueRule Field=”fax” Value=”null” InvertResult=”true”/>
</EnableRule>
And
<EnableRule Id=”Sample.account.form.CheckFaxValue.EnableRule”/>
<EnableRule Id=”Sample.account.form.CheckContactValue.EnableRule”/>
Below is the result:
When “Address Type” is selected as “Bill To(Value:1):
And when other than “Bill To” is selected:
This is fine and as expected. And one more thing we need to observe “EnableRule” placed under RibbonDiffXml/CommandDefinitions/CommandDefinition/EnableRules are by default uses “and” filter. If I remove “Fax” filed value and keep “Bill To” as the “Address Type”, the custom button will be disabled. Check the below screenshot:
So, what if we want to go with “or” condition??? Certainly, we do have “OrRule” which can be used for this purpose. Instead of having two separate “EnableRule” for each “ValueRule”, we can have one “EnableRule” with collection of “ValueRule” under “OrRule”. Below is the piece of code we need to have under //RibbonDiffXml/RuleDefinitions/EnableRules/EnableRule/:
<EnableRule Id=”Sample.account.form.OrRule.EnableRule”>
<OrRule>
<Or>
<ValueRule Field=”fax” Value=”null” InvertResult=”true”/>
</Or>
<Or>
<ValueRule Field=”address1_addresstypecode” Value=”1″/>
</Or>
</OrRule>
</EnableRule>
And refer the “Id” at the following: //CommandDefinitions/CommandDefinition/EnableRules
<EnableRule Id=”Sample.account.form.OrRule.EnableRule”/>
Below is the result with “OrRule”:
Now, the question arises what if I have complex query and based on its result custom button should be enabled/disabled??? The answer is, we do have one more feature called “CustomRule” using which we can call a Java Script function and based on the result(should be Boolean) we can enable/disable a custom button. In this coming post I will show how to use that Rule and in which scenarios it will be more useful.
This post gives basic idea on how to use “ValueRule” and “OrRule” in Ribbon Customizations. Hope it helps…!!! J
gud post
Good post, thank u 🙂
good one dear….
Good Post My Friend
Nice Post. Can we achieve this with field which is not included on the form but it is there in entity ? I mean that field is not visible over form.