ttajic
1/25/2019 - 11:28 AM

Dynamically Filter subgrid on form

function dynamicallyFilterSubGrid() {

    //RelatedStatement : is name of subgrid given on Form. 
    var objSubGrid =  window.parent.document.getElementById("statements");
    //var objSubGrid = Xrm.Page.ui.controls.get("statements");
    
    //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) {	
        setTimeout(dynamicallyFilterSubGrid, 2000);
        return;
    }

    try {
		var contact = Xrm.Page.getAttribute("parentcontactid").getValue();
		var account = Xrm.Page.getAttribute("parentaccountid").getValue(); 

		if (contact != null && account == null)
		{

		Xrm.Page.ui.controls.get("statements").setVisible(true);
		var contactid = contact[0].id;

		//Create FetchXML for sub grid to filter records based on category
		var FetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
						"<entity name='avt_pristopnaizjava'>" +
							"<attribute name='avt_st_pi'/>" +
							"<attribute name='avt_skladid'/>" +
							"<attribute name='avt_datumpristopa'/>" +
							"<attribute name='avt_svetovalecid'/>" +
							"<order descending='false' attribute='avt_st_pi'/>" +
							"<filter type='and'>" +
								"<condition attribute='statecode' value='0' operator='eq'/>" +
								"<condition attribute='avt_vlagateljfo' operator='eq' value='" + contactid + "' />" +
							 "</filter>" +
						"</entity>" +
					"</fetch>";

		// Layout of subgrid.
		var LayoutXml = "<grid name='resultset' object='8' jump='avt_st_pi' select='1' preview='1' icon='1'>" +
						" <row name='result' id='avt_pristopnaizjava'>" +
								"<cell name='avt_st_pi' width='100' />" +
								"<cell name='avt_skladid' width='200' />" +
								"<cell name='avt_datumpristopa' width='100' />" +
								"<cell name='avt_svetovalecid' width='100' />" +
							"</row>" +
					 "</grid>";

		//apply layout and filtered fetchXML
		objSubGrid.control.SetParameter("layoutXml", LayoutXml);
		objSubGrid.control.SetParameter("fetchXml", FetchXml);
		//document.getElementById("relatedactivities").control.SetParameter("fetchXML", fetchXml);
		//Xrm.Page.ui.controls.get("relatedactivities").refresh();

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

		}


		if (account != null && contact == null)
		{
		Xrm.Page.ui.controls.get("statements").setVisible(true);
		var accountid = account[0].id;

		//Create FetchXML for sub grid to filter records based on account
		var FetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
						"<entity name='avt_pristopnaizjava'>" +
							"<attribute name='avt_st_pi'/>" +
							"<attribute name='avt_skladid'/>" +
							"<attribute name='createdon'/>" +
							"<order descending='false' attribute='avt_st_pi'/>" +
							"<filter type='and'>" +
								"<condition attribute='statecode' value='0' operator='eq'/>" +
								"<condition attribute='avt_vlagatelj' operator='eq' value='" + accountid + "' />" +
							 "</filter>" +
						"</entity>" +
					"</fetch>";

		// Layout of subgrid.
		var LayoutXml = "<grid name='resultset' object='8' jump='avt_st_pi' select='1' preview='1' icon='1'>" +
						" <row name='result' id='avt_pristopnaizjava'>" +
								"<cell name='avt_st_pi' width='100' />" +
								"<cell name='avt_skladid' width='200' />" +
								"<cell name='createdon' 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();

		}


		if (account == null && contact == null)
		{
		Xrm.Page.ui.controls.get("statements").setVisible(false);
		}
    }
    catch(e)
    {
    }
}