Skip to main content

Notifications

Announcements

No record found.

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

    I have exact same reuirement. Were you able to resolve this? Please help!

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

    Hi Sagar , 

    I  have  got a similar issue :

    I have 2 entities ,  one is  list of countries  and other has list of users .

    On a third entity  i have the Country lookup  ,  the requirement is to filter the country lookup based on the logged in User and show only countries that this user has access to. 

    I find your issue is exactly same is mine , could you please reply how you have solved it using an intersect entity and help me. 

    Thanks.

  • razdynamics Profile Picture
    razdynamics 17,304 User Group Leader on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

    Hi Sagar, are you able to mark your case as answered so that it can be closed? Many Thanks

  • Sagar2016 Profile Picture
    Sagar2016 40 on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

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

  • Sagar2016 Profile Picture
    Sagar2016 40 on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

    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
    Sagar2016 40 on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

    I tried these.. not working..

  • Sagar2016 Profile Picture
    Sagar2016 40 on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Filter Lookup based on N:N relationship in MS CRM 2016

    Walkthrough the link provided

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

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,703 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,433 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans