Sonoma Partners Microsoft CRM and Salesforce Blog

How To Make Filtering of N:N Lookups Work for You

Today's blog post was written by Rob Montague, Senior Developer at Sonoma Partners.

Sometimes you need to filter a lookup that happens to be an N:N relationship. The issue that we as developers run into is that the control.addCustomFilter JavaScript function does not support N:N relationships. In this scenario, we had Manufacturer and Product entities in a N:N relationship, and we want to prevent the user from picking the wrong product line for a manufacturer. To do this, we created 6 different views on manufacturer entity, each view representing a different product line. The below supported code is an example of how we switched views to filter the Manufacturer lookup based on which Product line is selected.

function onLoad() {

Xrm.Page.getAttribute('sonoma_productLine').addOnChange(filterManufacturer);

           }

function filterManufacturer() {

            // Lookup Filter for Manufacturer field, filter by product Line with N:N relationship

            // needed to switch default views to make this work since N:N is not supporting filters

            var productLine = Xrm.Page.getAttribute('sonoma_productLine').getValue();

            if (productLine == null || productLine.length <= 0) {

                return;

            }

var audioFilterId ='{cb607e22-5403-e711-810e-e0071b6a9211}',

       caraudioFilterId = '{cee7cc88-5403-e711-810e-e0071b6a9211}',

       computerFilterId = '{9b74e0f3-5003-e711-810e-e0071b6a9211}',

       entertainmentFilterId = '{1f5f78d3-5303-e711-810e-e0071b6a9211}',

       videoFilterId = '{5ae0cb69-5403-e711-810e-e0071b6a9211}',

       videoGamesFilterId = '{d6c351ae-5303-e711-810e-e0071b6a9211}',

       defaultFilterId = '{205AC19F-F655-4580-B894-A1D33A6FC800}',

       manufacturerControl = Xrm.Page.getControl('sonoma_manufacturer');

            switch(productLine[0].name.toLowerCase()) {

                case 'audio': //View: Active Audio Manufacturers

                    manufacturerControl.setDefaultView(audioFilterId);

                    break;

                case 'video': //View: Active Video Manufacturers

                    manufacturerControl.setDefaultView(videoFilterId);

                    break;

                case 'car audio': //View: Active Car Audio Manufacturers

                    manufacturerControl.setDefaultView(caraudioFilterId);

                    break;

                case 'computer': //View: Active Computer Manufacturers

                    manufacturerControl.setDefaultView(computerFilterId);

                    break;

                case 'video games': //View: Active Video Game Manufacturers

                    manufacturerControl.setDefaultView(videoGamesFilterId);

                    break;

                case 'entertainment': //View: Active Entertainment Manufacturers

                    manufacturerControl.setDefaultView(entertainmentFilterId);

                    break;

                default: //View: Active Manufacturers

                    manufacturerControl.setDefaultView(defaultFilterId);

         break;

            }

        }

This simple solution ended up resolving the issue we ran into and since we used all supported code, this works on mobile as well. Let us know what solutions you have come up with for this problem by commenting below!

Topics: Microsoft Dynamics 365