Filtering records in the sub grid Based On OptionSet Selected Value in Microsoft Dynamics CRM 2011

Filtering records in the sub grid Based On Option Set Selected Value in Microsoft Dynamics CRM 2011

Let’s Take An Example . . Create an Option Set which contains the States  and place it on the Form.

Now insert the subgrid by clicking on the sub grid. Enter the name of the subgrid as “OrderStateCases”, select the entity as “Cases” as shown in the below figure and click on OK.

Now add the Javascript Web Resource code to the Entity and cal the Function in OnLoad

// JScript source code

function filterSubGrid() {

//debugger;

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

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

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

var optionSetField = new Array;

//Get the optionSetField

optionSetField = Xrm.Page.getAttribute(“new_state”).getValue();

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

if (optionSetField != null) {

var optionSetId = Xrm.Page.getAttribute(“new_state”).getValue();

}

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

else {

return;

}

//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(‘filterSubGrid()’, 100);

return;

}

//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.

relatedProducts.control.refresh();

}

Happy Coding.

Cheers,

Suresh Kumar D

Advertisements

Leave a Reply

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

WordPress.com Logo

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