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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Filter Lookup based on N:N relationship in MS CRM 2016

(0) ShareShare
ReportReport
Posted on by 40

I have created one N:N relationship between Contacts and Accounts.

On case form, I have account and contact lookups.

I want to filter contact lookup based on N:N relationship with account which is selected in lookup.

Also between account and contact, 1:N and N:1 relationship are present.

I have written following js. but not getting result.

I have added two records in N:N relationship.

function OnLoad() {
"use strict";
Xrm.Page.getControl("primarycontactid").addPreSearch(preFilterSearchOnContactLookup);
}

function preFilterSearchOnContactLookup() {
var customerAccount = Xrm.Page.getAttribute("customerid");
if (customerAccount != undefined && customerAccount != null) {
var customerAccountValue = customerAccount.getValue()[0];
if (customerAccountValue != undefined && customerAccountValue != null &&
customerAccountValue.id != undefined && customerAccountValue.id != null) {
var customerAccountId = customerAccountValue.id;
// use randomly generated GUID Id for our new view
var viewId = "{ad886ecf-d3e9-44f8-b9dd-3164d5d8b40d}";
var entityName = "contact";
// give the custom view a name
var viewDisplayName = "Related Contacts for " + customerAccountValue.name + "";
// find N:N related contacts
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"+
"<entity name='contact'>"+
"<attribute name='fullname' />"+
"<attribute name='contactid' />"+
"<order attribute='fullname' descending='false' />"+
"<link-entity name='new_account_contact' from='contactid' to='contactid' visible='false' intersect='true'>"+
"<link-entity name='account' from='accountid' to='accountid' alias='ab'>"+
"<filter type='and'>"+
"<condition attribute='accountid' operator='eq' value='" + customerAccountId + "' />" +
"</filter>"+
"</link-entity>"+
"</link-entity>"+
"</entity>"+
"</fetch>";
// build Grid Layout
var layoutXml = "<grid name='resultset' object='1' jump='contactid' select='1' icon='1' preview='1'>" +
"<row name='result' id='contactid'>" +
"<cell name='fullname' width='200' />" +
"</row>" +
"</grid>";
// add the Custom View to the indicated [lookupFieldName] Control
Xrm.Page.getControl("primarycontactid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}
else {
var defaultViewId = Xrm.Page.getControl("primarycontactid").getDefaultView();
Xrm.Page.getControl("primarycontactid").setDefaultView(defaultViewId);
}
}
else {
var defaultViewId = Xrm.Page.getControl("primarycontactid").getDefaultView();
Xrm.Page.getControl("primarycontactid").setDefaultView(defaultViewId);
}
}

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    hi,

    use pre lookup filter

    function preFilterLookup()

    {

    Xrm.Page.getControl("new_State").addPreSearch(function () {

    var stateObject = Xrm.Page.getAttribute("new_State").getValue();

    // Get the lookup Object, and extract the Value (ID and Text)

    if(stateObject != null)

    {

    var stateTextValue = stateObject[0].name;

    var stateID = stateObject[0].id;

    // Filter the cities by State

    fetchXml = "";

    // Apply the filter to the field

    Xrm.Page.getControl("new_City").addCustomFilter(fetchXml);

    }

    });

    }

  • Sagar2016 Profile Picture
    40 on at

    I have tried addCustomFilter function but link entity can not be used in fetch XML.

  • Sagar2016 Profile Picture
    40 on at

    If I have only 1:N relation, then addCustomFiler can be used.

  • Community Member Profile Picture
    on at

    Walkthrough the link provided

    www.magnetismsolutions.com/.../filter-n-n-add-existing-lookup-dynamics-crm-2011-rollup-12

  • Community Member Profile Picture
    on at

    The code looks correct for me, I can't debug this for now, so I suggest a workaround solution: use OData query to get the contactId from the N to N relationship, and use addcustomfilter with these contactids. Hope it helps.

  • Sagar2016 Profile Picture
    40 on at

    Hi.. I tried that also.. but I am getting both records' id using odata. But lookup doesn't show any record.

  • Sagar2016 Profile Picture
    40 on at

    I tried these.. not working..

  • Community Member Profile Picture
    on at

    I guess the addpresearch and addcustomview is not supported N to N maybe, you can get the accountid from Odata and use fetchXML like below:

    <filter type="and">

           <condition attribute="accountid" operator="in">

             <value uiname="Fourth Coffee (sample)" uitype="account">{DBBB8E56-B9D9-E311-9410-001CC4EECDD6}</value>

             <value uiname="Litware, Inc. (sample)" uitype="account">{DDBB8E56-B9D9-E311-9410-001CC4EECDD6}</value>

             <value uiname="Adventure Works (sample)" uitype="account">{DFBB8E56-B9D9-E311-9410-001CC4EECDD6}</value>

           </condition>

    </filter>

    I think this link may help you on this issue:

    missdynamicscrm.blogspot.com/.../crm-2013-using-addcustomfilter-to-get-filtered-lookup-field-based-on-linked-entity.html

  • Sagar2016 Profile Picture
    40 on at

    I tried this before.. Look up wont show result as expected.

  • Sagar2016 Profile Picture
    40 on at

    I found an alternate way to implement this using another intersect entity.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans