Filter Sub-Grid Dynamics CRM 2011 Rollup 12 (Polaris)

Paul Nieuwelaar, 24 January 2013

If you have used JavaScript to filter results in a sub-grid using pre-rollup 12 version of Dynamics CRM 2011, then you’re going to face problems with the move to Rollup 12 and Cross Browser. The first, and most obvious problem is the ‘setParameter’ function used to set the new fetchXML no longer exists.

 Filter Sub-Grid Dynamics CRM 2011 Rollup 12 (Polaris)

Thankfully, Greg from Vitalogy commented on a blog post by Vishnu Turlapati on how to filter sub-grids before rollup 12 (using the deprecated ‘setParameter’). Greg explained what must now be done to get this working with rollup 12, which is to simply use ‘SetParameter’ instead of ‘setParameter’ (using an uppercase ‘S’).

We are also faced with another issue, in that ‘element.readyState’ is not supported cross-browser, so this needs to be removed. The complete updated code to use for rollup 12 versions of CRM cross-browser is below.

In this example, I’ve added an ‘accounts’ sub-grid to my Account form, to display all the Accounts that the Primary Contact is a Primary Contact of (which will obviously include the current account). This function can be added to the form OnLoad and Primary Contact field OnChange to ensure the sub-grid is updated when the Primary Contact changes as well.

NOTE: I’ve found it best to create a custom view to be used as the default sub-grid view; and to customize this view to display no results by default (using a filter such as: Name equals “X” and Name does not equal “X” – which will always return no results), this way you won’t see incorrect data initially on load before the JavaScript kicks in.

function filterSubGrid() {
    var accountsGrid = document.getElementById("accounts"); //grid to filter 
    if (accountsGrid == null) { //make sure the grid has loaded 
        setTimeout(function () { filterSubGrid(); }, 2000); //if the grid hasn’t loaded run this again when it has 
        return;
    }

    var contactValue = Xrm.Page.getAttribute("primarycontactid").getValue(); //field to filter by 

   
var contactId = "00000000-0000-0000-0000-000000000000"; //if filter field is null display nothing 
    if (contactValue != null) {
        var contactId = contactValue[0].id;
    }

    //fetch xml code which will retrieve all the accounts related to the contact 
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
    "  <entity name='account'>" +
    "    <attribute name='name' />" +
    "    <attribute name='address1_city' />" +
    "    <attribute name='primarycontactid' />" +
    "    <attribute name='telephone1' />" +
    "    <attribute name='accountid' />" +
    "    <order attribute='name' descending='false' />" +
    "    <filter type='and'>" +
    "      <condition attribute='primarycontactid' operator='eq' uitype='contact' value='" + contactId + "' />" +
    "    </filter>" +
    "    <link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='accountprimarycontactidcontactcontactid'>" +
    "      <attribute name='emailaddress1' />" +
    "    </link-entity>" +
    "  </entity>" +
    "</fetch>";

    accountsGrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid   
    accountsGrid.control.refresh(); //refresh the sub grid using the new fetch xml
}  

Credit to Greg from Vitalogy for discovering the new ‘SetParameter’ function, and credit to Vishnu Turlapati for writing the initial blog post for pre rollup 12.