web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Filter Lookup with a N:N relationship

(0) ShareShare
ReportReport
Posted on by 393
Hi all, we are trying to filter a lookup that has a N:N relationship to another entity but seems like the filtering is not working. We have the below: 
 
  • Entity named Industry (example; Industry A, Industry B)
  • Entity named Applications (example; Application A, Application B, Application C, Application D)
    • This entity has a N:N (many to many) reationship with Industry. 
  • Extended the standard quote entity with;
    • a new lookup to the industry entity, exposed in the Quote Main form
    • a new lookup to the applications entity, exposed in the Quote Main form
 
This means that in the quote form, the user selects the Industry first and then, the Application lookup should be automatically filtered to show only applications related to the selected entity. 
 
We created a Web Resource in which we have 2 functions as per below. On the event OnChange of the Industry lookup field in the quote main form, we are calling the setPreSearch function. 
Unfortunately the application lookup is not being filtered. Any guesses to what's wrong here? We've debugged the function filterCustomerApplicationsbyIndustry and can see that the filterXML is being done correctly but somehow seems like it is being lost between the functions calling each other. 
 
We have also tried calling the filterCustomerApplicationsbyIndustry function in the setPreSearch function like this, but has not worked either.
attribute.controls.forEach(control => control.addPreSearch(PublisherA.IndustryApplication.filterCustomerApplicationsbyIndustry(executionContext))) 
 
 
Below is the javascript we did: 
 
(function () {
    PublisherA.IndustryApplication.setPreSearch = function (executionContext) {
        debugger;
        var formContext = executionContext.getFormContext()
        var attribute = formContext.getAttribute("pubA_customerapplication")
        attribute.controls.forEach(control => control.addPreSearch(PublisherA.IndustryApplication.filterCustomerApplicationsbyIndustry(executionContext))) 
    }

    PublisherA.IndustryApplication.filterCustomerApplicationsbyIndustry = function (executionContext) {
        var formContext = executionContext.getFormContext();
        var entityName = formContext.data.entity.getEntityName();

        // Replace these with actual logical names on each entity form
        var industryFieldLogicalName;
        var customerAppFieldLogicalName;

        switch (entityName) {
            case "quote":
                industryFieldLogicalName = "pubA_industry"; //field schema name on Quote
                customerAppFieldLogicalName = "pubA_customerapplication"; //field schema name on Quote
                break;

            case "order":
                industryFieldLogicalName = "pubA_industry"; // field schema name on Order
                customerAppFieldLogicalName = "pubA_customerapplication"; // field schema name on Order
                break;

            default:
                console.log("Entity not supported for filtering: " + entityName);
                return;
        }

        var industryAttribute = formContext.getAttribute(industryFieldLogicalName);
        var customerAppControl = formContext.getControl(customerAppFieldLogicalName);

        if (!industryAttribute || !customerAppControl) {
            console.error("Field logical names are incorrect or controls not found.");
            return;
        }

        var industryValue = industryAttribute.getValue();
        if (!industryValue || industryValue.length === 0) {
            // Clear filters if no industry selected
            customerAppControl.clearCustomFilter();
            return;
        }

        var industryId = industryValue[0].id.replace("{", "").replace("}", "");
        debugger;
        // Replace below tokens with your actual metadata logical names:
        var customerAppEntityLogicalName = "pubA_customerapplicationindustry";          // table logical name
        var customerAppPkLogicalName = "pubA_customerapplicationindustryid";        // PK of Customer Application
        var industryPkLogicalName = "pubA_industryid";                   // PK of Industry        
        var nnNavigationPropertyOnApp = "pubA_CustomerApplicationIndustry_pubA_Industry_pubA_Industry"; //relationship schema name

        var query =
            "?$select=" + customerAppPkLogicalName +
            "&$expand=" + nnNavigationPropertyOnApp + "($select=" + industryPkLogicalName + ")" +
            "&$filter=" + nnNavigationPropertyOnApp + "/any(i:i/" + industryPkLogicalName + " eq " + industryId + ")";

        Xrm.WebApi.retrieveMultipleRecords(customerAppEntityLogicalName, query).then(
            function (result) {
                if (result.entities.length > 0) {
                    var filterXml = "<filter type='and'><condition attribute='" + customerAppPkLogicalName + "' operator='in'>";
                    result.entities.forEach(function (app) {
                        filterXml += "<value>" + app[customerAppPkLogicalName] + "</value>";
                    });
                    filterXml += "</condition></filter>";

                    customerAppControl.addCustomFilter(filterXml, customerAppEntityLogicalName);
                } else {
                    // No matches; clear filter
                    customerAppControl.clearCustomFilter();
                }
            },
            function (error) {
                console.error(error.message);
            }
        );
    }
})();
 
Thanks in advance for your help! 
 
 
 
Categories:
I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 93

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 65 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans