I have found quite a few helpful resources about the AddCustomFilter method in jScript that can help us to filter a lookup in ways that OOB CRM cannot. I have been tackling this issue for a while, and have gone at it in a number of different ways.
Here is the requirement:
On a Work Order Product, the Product Lookup must be filtered to show only Products that belong to the PriceList which is set on the Work Order. Using related records (OOB) filtering, I can only achieve this by using the Default Price List of a product - which will not work for my requirement.
Basically, I want the Product Lookup to only return Products that exist in the Price List that is set on the Work Order.
Right now, I am getting *some type* of filtered list, but it isn't correct. And further, inside the lookup window (where you can change the views, do search, etc), if I click the Magnifying Glass (Search) icon twice, the 'filter' (which again is not correct) disappears, and then a list of ALL FS Products is shown. Why is that?
Here is my code. This is the simplest implementation I've tried over a number of attempts. I just recently added the Try/Catch blocks, since this was so dang simple, I wasn't thinking i'd need them. But at this point, I am not catching any errors, no errors in F12 Debug, and no errors popped by CRM UI.
Any thoughts on why this his happening?
function FilterWOProductList() { var PriceList = Xrm.Page.getAttribute("msdyn_pricelist").getValue(); //checking if pricelist fields is empty before we apply the filter if (PriceList != null) { Xrm.Page.getControl("msdyn_product").addPreSearch(Filter); } } function Filter() { var PriceListValue = Xrm.Page.getAttribute("msdyn_pricelist").getValue(); var PriceList = Xrm.Page.getAttribute("msdyn_pricelist").getValue(); //if PriceList has a value, proceed if (PriceList != null) { //used to retrieve Name of the Price List held in the PriceList field var PriceListTextValue = PriceListValue[0].name; //GUID used in filter (pricelist GUID) var PriceListID = PriceListValue[0].id; try { var plist_filter = "<filter type='and'>" + "<condition attribute='msdyn_pricelist' operator='eq' value='" + PriceListID + "' />" + "</filter>"; Xrm.Page.getControl("msdyn_product").addCustomFilter(plist_filter, "msdyn_product"); } catch (e) { Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message)); } } }