Filtering records in the sub grid Based on LookUp Value in Microsoft Dynamics CRM 2011.

Filtering records in the sub grid  Based on LookUp Value in Microsoft Dynamics CRM 2011.

CRM 2011 has introduced new concept of attaching sub grid to an entity form. Ever tried filtering the grid based on the value present in the form? This post will help you in achieving the requirement.


Filtering sub grid records based on a condition.

Steps to be followed:

1) Open the case entity in the settings customization area and create a many to 1 relationship between order and case as shown in the below figure.

2) Now click on forms and open the main form as shown in the below figure.

3) Now add the order lookup field into the form and insert the subgrid by clicking on the sub grid. Enter the name of the subgrid as “OrderProducts”, select the entity as “Order Products” as shown in the below figure and click on OK.

4) After adding the required fields our form looks as shown in the below figure.

5) By default the subgrid shows all the order products available in the CRM application. Our target is to fetch all the order products related to the order selected in the case form. To achieve that we have to write a small code snippet which will meet the requirement. Just click on the form in the customize tab which will open the case entity form as shown in the below figure.

7) Now in the edit library use the following code which will meet the requirement of filtering the order products based on the order selected in the case record form.

// JScript source code

function updateSubGrid() {

//This will get the related products grid details and store in a variable.

var relatedProducts = document.getElementById(“OrderProducts”);

//Initializing the lookup field to store in an array.

var lookupfield = new Array;

//Get the lookup field

lookupfield = Xrm.Page.getAttribute(“new_orderid”).getValue();

//This will get the lookup field guid if there is value present in the lookup

if (lookupfield != null) {

var lookupid = lookupfield[0].id;


//Else the function will return and no code will be executed.

else {



//This method is to ensure that grid is loaded before processing.

if (relatedProducts ==null || relatedProducts.readyState != “complete”)


//This statement is used to wait for 2 seconds and recall the function until the grid is loaded.

setTimeout(‘updateSubGrid()’, 2000);



//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=’salesorderdetail’>”;

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

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

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

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

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

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

fetchXml += “<order attribute=’productid’ descending=’false’ />”;

fetchXml += “<link-entity name=’salesorder’ from=’salesorderid’ to=’salesorderid’ alias=’aa’>”;

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

fetchXml += “<condition attribute=’salesorderid’ operator=’eq’ uiname=’Order Name’ uitype=’salesorder’ value='” + lookupid + “‘ />”;

fetchXml += “</filter>”;

fetchXml += “</link-entity>”;

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.



8) Make sure that the function updateSubGrid is used in the form load as shown in the below figure. To get this form click on edit button in the form properties.

9) After making all the modifications click on OK and publish the entity customizations.

10) This change will retrieve all the order products related to order selected in the case.


Url Taken From


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