Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

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

$
0
0

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);
}
}


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>