How To – Series 6: How To Use “CustomRule” to Enable/Disable HomePageGrid Buttons- CRM 2011

 

In the earlier post here we have seen how to use <ValueRule> to retrieve a field value from the CRM form and Enable/Disable Custom Button based on that value. What if we want to do the same thing with “HomePageGrid” also like based on a particular value from the selected record in the Grid, Enable/Disable a Custom Button? Can we use <ValueRule> for the same?? The answer is big “No…!!!” Instead, we can use <CustomRule> wherein we can call a JavaScript function to do the required stuff.
For this post also, I am doing required stuff for “HomePageGrid” on top of the following SDK walkthrough: Walkthrough: Add a Custom Button to an Existing Group for a Specific Entity . Below is the result of SDK sample for Account Home Page Grid:
Custom button “Hello Ribbon” will be enabled when only one Account record is selected. Following is the code snippet for that:
<EnableRule Id=”Sample.account.grid.OneSelected.EnableRule”>
  <SelectionCountRule AppliesTo=”SelectedEntity” Maximum=”1″ Minimum=”1″ />
</EnableRule>
Now, let’s consider a scenario where we want to enable the “Hello Ribbon” button when only one Account record is selected and the selected record should have “Fax” value. We will see what are all the steps we need to follow to accomplish this.
Step 1: Retrieve the selected Item’s GUID value
Step2: Use the resultant GUID value to retrieve “Fax” field value using “REST” or “SOAP” calls
Step 1:
Inorder to retrieve selected item’s GUID value, we can use the following:
<CrmParameter Value=”SelectedControlSelectedItemCount” />
<CrmParameter Value=”FirstSelectedItemId” />
The first one returns an array of GUIDs for the selected items and the latter one returns GUID of the first item in the selected items.
<CrmParameter Value=”SelectedControlAllItemCount” />
Using this one we can get a count of selected items.
Step 2:
Use <CustomRule> to call java script function by passing the selected GUID value and it’s count. Following is the code snippet to do that:
            <EnableRule Id=”Sample.account.grid.SelectedRowValueCheck.EnableRule”>
              <CustomRule FunctionName=”IsExist” Library=”$webresource:new_CheckFieldValue.js”>
                <CrmParameter Value=” SelectedControlSelectedItemCount” />
                <CrmParameter Value=”FirstSelectedItemId” />
              </CustomRule>
            </EnableRule>
Here, “FunctionName” refers to Java Script method and “Library” refers to Java Script web resource. And the <CrmParameter> defined will pass it’s returned Value to the <CustomRule>’s function as parameters.
Let’s go define “IsExist” method in “new_CheckFiledValue.js” web resource as follows:
function IsExist(rowscount, firstselectedid) {
    if (rowscount == 1) {
        var ValueExist = ReturnValue(firstselectedid);
        return ValueExist;
    }
    else {
        return false;
    }
}
Observe the “IsExist” function’s prototype. It has two parameters. The first one “rowscount” refers to first <CrmParameter> and the second one referes to second <CrmParameter>. In the definition, I am calling a method “ReturnValue” with “firstselectedid” as the parameter when “rowscount” is 1. If not, returning “false”. In the “ReturnValue” method I am planning to retrieve “Fax” field value based on the GUID from the “firstselectedid” parameter using REST based web service call.
function ReturnValue(firstselectedid) {
    var context = Xrm.Page.context;
    var serverUrl = context.getServerUrl();
    var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;
    var ODATA_Filter = “/AccountSet?$select=Fax&$filter=AccountId/Id eq (guid'” + firstselectedid + “‘)”;
    var jsonEntity = window.JSON.stringify(CRMObject);
    $.ajax({ type: “GET”,
        contentType: “application/json; charset=utf-8”,
        datatype: “json”,
        url: serverUrl + ODATA_ENDPOINT + ODATA_Filter,
        beforeSend: function (XMLHttpRequest) {
             XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);
        },
        success: function (data, textStatus, XmlHttpRequest) {
            var faxValue = data[“d”];
            if (faxValue != null)
                return true;
            else
                return false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            return false;
        }
    });
}
This function returns “True” if “Fax” field has value else “False”.
PS: We need to refer “JQuery” and “JSON” libraries in our Java Script web resource to use REST end points. However, I am still struggling to find out how to refer “JQuery” and “JSON” libraries in our Java script web resource as we are not calling the functions as part of any event. Right now, I am working on an “Online” deployment. If it is On-Premise environment, we could easily call “JQuery” and “JSON” libraries as external files the same way we used to do in CRM 4.0. Anyways, I will come back here once I find a solution.
Hope it helps in giving a basic idea on how to use <CustomRule> and how to Enable or Disable HomePageGrid’s Button based on a condition.

4 thoughts on “How To – Series 6: How To Use “CustomRule” to Enable/Disable HomePageGrid Buttons- CRM 2011

  1. Pranav Shah

    I have come across similar problem and resolved it by loading JavaScript inside Actions node of the Command like this. It works for me on both GridView as well as Form Ribbons. In following example I have implemented a rule for "Resolve Case" button on Case grid view as well as form

  2. Bill Faulk

    If you already have json/jquery in your form's libraries, and you aren't using the customrule on a grid, then the querying works fine with no additional resources. For a grid, I'd probably include the javascript from a web resource as you mentioned.

    Also, you'd need to add a line to declare CRMObject I think, i.e. "var CRMObject = new Object();". For my version using jquery 1.4.1 I had to refer to results like
    var faxValue = data["d"].results[0].Fax;
    rather than just
    faxValue = data["d"];

Comments are closed.