Filter Look Up on Contact on Account basis where account is parent or contact is primary contact on account

Posted on Posted in JavaScript, Microsoft, MS CRM 2016, MS Dynamics

In this scenario, I have a requirement where I user wants to select Account on Opportunity entity

On selection of account user only wants to see those Contacts which are child records of selected account or a contact which is primary contact on selected account.

If it was just child account we could just achieve this using filtered views however to add two conditions it was not possible.

We can use addCustomFilter using JavaScript

In function below we are getting out contact control and then calling an other function

function preFilterLookup() {
Xrm.Page.getControl(“header_process_parentcontactid”).addPreSearch(function () {
addLookupFilterOnContact();
});
}

Below is complete code to get Primary Contact on selected account and then modifying Fetch XML and then passing it to custom filter

function addLookupFilterOnContact() {
var parentaccount = Xrm.Page.getAttribute(“parentaccountid”).getValue();
var primaryContactId = ”;
var accountId = parentaccount[0].id;

var req = new XMLHttpRequest();
req.open(“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v8.2/accounts(” + accountId + “)?$select=_primarycontactid_value”, true);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“Prefer”, “odata.include-annotations=\”*\””);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {

var result = JSON.parse(this.response);
primaryContactId = result[“_primarycontactid_value”];
if (parentaccount != null) {
var parentaccountid = parentaccount[0].id
if (parentaccountid != null && primaryContactId != null) {
fetchXml = “<filter type=’or’><condition attribute=’parentcustomerid’ value='” + parentaccountid + “‘ operator=’eq’/><condition attribute=’contactid’ value='{” + primaryContactId + “}’ operator=’eq’/></filter>”;

Xrm.Page.getControl(“header_process_parentcontactid”).addCustomFilter(fetchXml);
}
}

} else {
if (parentaccount != null) {
var parentaccountid = parentaccount[0].id
if (parentaccountid != null && primaryContactId != null) {
fetchXml = “<filter type=’and’><condition attribute=’parentcustomerid’ operator=’eq’ value='” + parentaccountid + “‘ /></filter>”;

Xrm.Page.getControl(“header_process_parentcontactid”).addCustomFilter(fetchXml);
}
}
}
}
};
req.send();

if (parentaccount != null) {
var parentaccountid = parentaccount[0].id
if (parentaccountid != null && primaryContactId != null && (primaryContactId)) {
fetchXml = “<filter type=’or’><condition attribute=’parentcustomerid’ value='” + parentaccountid + “‘ operator=’eq’/><condition attribute=’contactid’ value='{” + primaryContactId + “}’ operator=’eq’/></filter>”;
Xrm.Page.getControl(“header_process_parentcontactid”).addCustomFilter(fetchXml);
}
}
}