Filtered Custom Subgrid Based on Dynamic Value


Filtered Subgrid in MSCRM using Javascript

If you have two entities without any relation to filter your sub grid then you can use the custom filtered sub grid.

function dynamicallyFilterSubGrid() {
    debugger;
    var objSubGrid = document.getElementById("Quotes"); //The name of the subgrid

    //CRM loads subgrid after form is loaded. so when we are adding script on form load.. need to wait unitil subgrid is loaded. 
    // thats why adding delay..
    if (objSubGrid == null) {// || objSubGrid.readyState != "complete"
        setTimeout(dynamicallyFilterSubGrid, 2000);
        return;
    }

    else {
        //when subgrid is loaded, get field value on which we are filtering
        var oppurtunity = Xrm.Page.getAttribute('new_oppurtunityid').getValue();
        // var oppId = "{1DAEF0BB-34E2-E611-8164-00155D55CB07}";
        var oppId = "{00000000-0000-0000-0000-000000000000}";// If you are not using related record while creating subgrid
        if (oppurtunity != null) {
            var FetchOppXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
                "<entity name='opportunity'>" +
                "<attribute name='name'/>" +
                "<attribute name='customerid'/>" +
                "<attribute name='opportunityid'/>" +
                "<order descending='false' attribute='name'/>" +
                "<filter type='and'>" +
                "<condition attribute='new_opportunityid' value='" + oppurtunity + "' operator='eq'/>" +
                "</filter>" +
                "</entity>" +
                "</fetch>";
            var Results1 = XrmServiceToolkit.Soap.Fetch(FetchOppXml);
            if (Results1.length > 0) {
                oppId = Results1[0].attributes.opportunityid.value;
            }
        }

        //Create FetchXML for sub grid to filter records based on retrieved values
        var FetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
            "<entity name='quote'>" +
            "<attribute name='customerid'/>" +
            "<attribute name='new_typeofplan'/>" +
            "<attribute name='statuscode'/>" +
            "<attribute name='quotenumber'/>" +
            "<attribute name='opportunityid'/>" +
            "<attribute name='createdon'/>" +
            "<attribute name='quoteid'/>" +
            "<order descending='true' attribute='createdon'/>" +
            "<filter type='and'>" +
            "<condition attribute='opportunityid' operator='eq' value='" + oppId + "' />" +
            "</filter>" +
            "</entity>" +
            "</fetch>";

        // Layout of subgrid.
        var LayoutXml = "<grid name='resultset' object='8' jump='new_name' select='1' preview='1' icon='1'>" +
            " <row name='result' id='new_boat'>" +
            "<cell name='quotenumber' width='100' />" +
            "<cell name='new_typeofplan' width='200' />" +
            "<cell name='statuscode' width='200' />" +
            "<cell name='customerid' width='100' />" +
            "<cell name='createdon' width='100' />" +
            "<cell name='opportunityid' width='100' />" +
            "</row>" +
            "</grid>";

        //apply layout and filtered fetchXML
        objSubGrid.control.SetParameter("layoutXml", LayoutXml);
        objSubGrid.control.SetParameter("fetchXml", FetchXml);

        //Refresh grid to show filtered records only. 
        objSubGrid.control.Refresh();

    }
}