How to Filter SubGrid Lookup view in Dynamics 365 CRM.
Please check the comments in the below code and do follow the steps accordingly and call the filterSubGrid() funtion on onload.
var LastQuery = ""; function filterSubGrid() { debugger; setSubgridLookupFiltering(); } function AddLookupFilter(entity_type, customFilter) { var subgridLookup = Xrm.Page.getControl("lookup_Contacts_Participants"); subgridLookup.addCustomFilter(customFilter, entity_type); } function setSubgridLookupFiltering() { var subgridAddButtonId = "Contacts_Participants_addImageButton"; //Try to get the element from both the current and the parent document. var subgridAddButton = document.getElementById(subgridAddButtonId) || window.parent.document.getElementById(subgridAddButtonId); //This script may run before the subgrid has been fully loaded on the form. If this is the case, //delay and retry until the subgrid has been loaded. if (subgridAddButton == null) { setTimeout(setSubgridLookupFiltering, 2000); return; } //Local function to retrieve the lookup control and apply the filter. We will queue this function in the click event handler of the //Add button's click event. var getSubgridLookupAndAddFilter = function() { var subgridLookup = Xrm.Page.getControl("lookup_Contacts_Participants"); //Delay and retry until we can locate the lookup. if (subgridLookup == null) { setTimeout(getSubgridLookupAndAddFilter, 200); return; } //This is a custom property we are tagging on to the lookup control to ensure that we will //apply the custom filter only once when the Add button is first clicked. if (subgridLookup.customFilterAdded) { subgridLookup.removePreSearch(function () { subgridLookup.addCustomFilter(LastQuery, "contact"); //AddLookupFilter("contact", LastQuery); }); } var LookUp = Xrm.Page.getAttribute("jar_cliente").getValue(); console.log(LastQuery); subgridLookup.addPreSearch(function() { //Standard logic to build up the filter query string var filterQuery = "<filter type='and'><condition attribute='parentcustomerid' operator='eq' value='" + LookUp[0].id + "' /></filter>";
//Set Global Variable to remove PreSearch
LastQuery = filterQuery; //Standard call to add filter to the lookup control subgridLookup.addCustomFilter(filterQuery, "contact"); }); //Mark the fact that we have already added the filter so that we won't do it again the next time the user clicks the Add button. subgridLookup.customFilterAdded = true; }; //Attach the function to retrieve the lookup and apply the filter to the Add button's click event. Remember that we //can only get the lookup after the user has clicked the Add button. subgridAddButton.addEventListener("click", function() { setTimeout(getSubgridLookupAndAddFilter, 200); }); }
Only a one problem, when the user change the principal lookup field, not work the filter, the function is called removePreSearch.
if (subgridLookup.customFilterAdded) { subgridLookup.removePreSearch(function () { subgridLookup.addCustomFilter(LastQuery, "contact"); //AddLookupFilter("contact", LastQuery); }); }
link: https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/261801/filter-new-record-subgrid-with-field
Comments
Post a Comment