Changing the SubGrid FetchXml in MS CRM 2011

Changing the SubGrid FetchXml in MS CRM 2011

I had a requirement to show a list of leads which have the same company name as the current lead. A subgrid seemed to be the way forwards, in CRM 4.0 I’d have added an IFrame displaying an advanced find list, but the limitation of only displaying related records added something of a stumbling block.
With a bit of, unsupported, scripting I managed to work around this issue.

Here’s how:
1. Add a subgrid to the Lead form showing all leads. Take a note of the subgrid name.
2. Generate the FetchXML that you want to inject into the subgrid (Advanced Find is your friend here).
3. The following JScript method demonstrates changing the FetchXml for a subgrid called “RelatedLeads”

function UpdateSubGrid() 
 var leadGrid = document.getElementById("RelatedLeads");

 //If this method is called from the form OnLoad, make sure that the grid is loaded before proceeding 
  //Included extra null check as a rollup 5 fix 
 if (leadGrid ==null || leadGrid.readyState != "complete") 
  //The subgrid hasn't loaded, wait 1 second and then try again     
 setTimeout('UpdateLeadSubGrid()', 1000);     
 //Update the fetchXML that will be used by the grid.  
var fetchXml = "";  
fetchXml += "  ";  
fetchXml += "    ";  
fetchXml += "    ";  
fetchXml += "    ";  
fetchXml += "    ";  
fetchXml += "    ";  
fetchXml += "    ";  
fetchXml += "    ";  
fetchXml += "    "; 
fetchXml += "      ";  
fetchXml += "    ";  
fetchXml += "  ";  
fetchXml += "";
 //Inject the new fetchXml  
leadGrid.control.setParameter("fetchXml", fetchXml);  
//Force the subgrid to refresh  

leadGrid.control.refresh(); }

//For Example

//This is the fetch xml code which will retrieve all the order products related to the order selected for the case.

var fetchXml = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>”;

fetchXml += “<entity name=’incident’>”;

fetchXml += “<attribute name=’title’ />”;

fetchXml += “<attribute name=’ticketnumber’ />”;

fetchXml += “<attribute name=’createdon’ />”;

fetchXml += “<attribute name=’new_state’ />”;

fetchXml += “<filter type=’and’>”;

fetchXml += “<condition attribute=’new_state’ operator=’eq’ value='” + optionSetId + “‘ />”;

fetchXml += “</filter>”;

fetchXml += “</entity>”;

fetchXml += “</fetch>”;

//Setting the fetch xml to the sub grid.

relatedProducts.control.setParameter(“fetchXml”, fetchXml);

//This statement will refresh the sub grid after making all modifications.



4. As a minimum the method can be called from the OnLoad event but it’s probably best to call it from the OnChange event of any attributes that are used to generate the FetchXML



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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