Mohsin Tester Mohsin Tester - 1 month ago 16
Javascript Question

Show specific account records in account lookup in Dynamics CRM

I've N:N relationship between account and Relationship Type (A custom entity). Now on Recording entity (Another custom entity) I've an account lookup and I want to add presearch criteria so that the lookup can show only the specific that have 'Vendor' relation in relationship type. So far, I've following snippet to make the lookup presearch but it shows all records. I am not sure where I am doing wrong. Any Idea/suggestion?

function filterAccounts() {
try {
debugger;
var accountLookup = Xrm.Page.getControl("new_accountid");
if (accountLookup == null && accountLookup == 'undefined') { }
else {
accountLookup.addPreSearch(function () {

CustomFilter(accountLookup);
});
}

} catch (e) {
alert("Error: " + e.message);
}

}

function CustomFilter(accountLookup) {
try {
debugger;
var fetchXml = "<link-entity name='new_account_new_relationshiptype' from='accountid' to='accountid' visible='false' intersect='true'>" +
" <link-entity name='new_relationshiptype' from='new_relationshiptypeid' to='new_relationshiptypeid' alias='ak'>" +
" <filter type='and'>" +
" <condition attribute='new_name' operator='eq' value='Vendor' />" +
" </filter>" +
" </link-entity>" +
" </link-entity>";

accountLookup.addCustomFilter(fetchXml);
} catch (e) {
alert("Error: " + e.message);

}
}

Answer

The FetchXML you specify in addCustomFilter is only meant to be the <filter>-part. You cannot specify link entities when using addCustomFilter.

If you cannot simplify your query such that you only have to consider fields on the Account entity, you will have to use addCustomView instead of addCustomFilter.