How can I filter Lookups using dependant option sets?

90 views Asked by At

I'm trying to filter to a product to make searching for a specific product easier for users. The goal is to have a chain of dependent option sets that ends in a lookup field that only displays records with the previous values. The products are all textbooks. The chain goes as such:

-Grade -Language -Editorial -Component (eg. textbook, workbook, atlas, etc) -Product Lookup

All the previous values are option sets that are available on the Product form because each Textbook has a grade level, language, editorial, and component.

I've found how to implement dependent option sets and filtered lookups online but I can't find a way to relate the two. I tried using only filtered lookups but it seems that would only work if I create separate entities for Grade, Language, Editorial, and Component and relate them all.

Does anybody have an idea of how I can achieve my goal?

1

There are 1 answers

1
S.Dav On

You can retrieve the values of the Option sets and then use the addPreSearch method to filter the lookup.

Your code could be similar to the code below:

function preFilterLookup() {
Xrm.Page.getControl("productfield").addPreSearch(function () {
    addLookupFilter();
});
}

function addLookupFilter() {
var grade = Xrm.Page.getAttribute("gradefield").getValue();
var language = Xrm.Page.getAttribute("languagefield").getValue();
var editorial = Xrm.Page.getAttribute("editorialfield").getValue();
var component = Xrm.Page.getAttribute("component").getValue();

var fetchXml = "";

if (grade != null)
    fetchXml += "<filter type='and'><condition attribute='gradefield' operator='eq' value='" + grade + "' /></filter>";

if (language != null)
    fetchXml += "<filter type='and'><condition attribute='languagefield' operator='eq' value='" + language + "' /></filter>";

if (editorial != null)
    fetchXml += "<filter type='and'><condition attribute='editorialfield' operator='eq' value='" + editorial + "' /></filter>";

if (component != null)
    fetchXml += "<filter type='and'><condition attribute='component' operator='eq' value='" + component + "' /></filter>";

Xrm.Page.getControl("productfield").addCustomFilter(fetchXml);
}