locked
Custom filter not working RRS feed

  • Question

  • I have a lookup to product on an opportunity. I want the product lookup to be filtered based on product type, once a type is chosen from a drop down on the opportunity form. I'm using .addCustomFilter and .addPreSearch, but it's not working. I've seen a few blogs that say to also use .preFilterSearch, but none of them say how to use it or what to do with it...they just say "use it". I can't find anything more on .preFilterSearch.

    Here is my code so far. The only alert that is displayed is the one that says "Calling filter".

    // function to call on change of Product Type
    function filterLookup() {
    	try {
    		
    		if (Xrm.Page.getControl("co_prodType" )!= null && Xrm.Page.getControl("co_prodType") != undefined) {
    			Xrm.Page.getControl("co_product").addPreSearch(function () {
    				alert("Calling filter"); // Displayed when creating new opportunity and clicking on lookup
    				// calling filter method
    				addCustomLookupFilter();
    			});		
    		}		
    	} catch (e) {
    		
    		throw new Error(e.Message);
    	}
    }
    
    function addCustomLookupFilter() {
    	alert("Here 1"); // Is not displayed
    	
    	try {
    		
    		var recordId = Xm.Page.data.entity.getId();
    		alert("recordId: " + recordId); // Is not displayed
    		
    		var prodType = Xrm.Page.getAttribute("co_prodType").getValue();
    		var prodTypeCodeToSearchFor;
    		switch(prodType) {
    			case 100000000: 			
    				prodTypeCodeToSearchFor = 1; 
    				break;
    			case 100000001:	
    				prodTypeCodeToSearchFor = 2; 
    				break;			
    			default:
    				prodTypeCodeToSearchFor = 1; 
    				break;
    		}
    		
    		if (prodType != null && prodType != undefined) {
    			// Prepare condition for the filter
    			fetchXML = "<filter type='and'><condition attribute='producttypecode' operator='eq' value='%" + prodTypeCodeToSearchFor + "%' /></filter>";			
    			// Apply filter to the lookup field
    			Xrm.Page.getControl("co_product").addCustomFilter(fetchXml);
    		}
    	}
    	catch (e) {	
    		throw new Error(e.Message);
    	}
    	
    }

    Monday, July 17, 2017 11:40 AM

All replies

  • Have you reviewed this on MSDN, https://msdn.microsoft.com/en-us/library/gg334266.aspx#BKMK_lookupControlMethods?

    Monday, July 17, 2017 2:02 PM
    Moderator