Search code examples
javascriptdynamics-crm

search filter on lookup field in ms crm


I have two entities: First I have 'contact' entity which contains both individuals and employees of company. I also have entity type 'account' which represents companies in my crm system.

I want to add filter to my customer search that contains:

  1. contact entity that equals customertypecode=1 (individuals but not company employee)
  2. All accounts

This is working only for contacts individual's:

var fetchXml = "<filter><condition attribute='customertypecode' operator='eq' value='1' /></filter>";
Xrm.Page.getControl("customerlookupfield").addCustomFilter(fetchXml);

But I also want it to show companies (account entity), could you help?


Solution

  • Instead have 2 filters & add it to respective custom filter. Hence search result will be filtered.

    var contactFilter = "<filter type='and'><condition attribute='customertypecode' operator='eq' value='1' /></filter>";
    var accountFilter = "<filter type='and'><condition attribute='accountid' operator='not-null' /></filter>";
    
    Xrm.Page.getControl("customerlookupfield").addCustomFilter(contactFilter, "contact");
    Xrm.Page.getControl("customerlookupfield").addCustomFilter(accountFilter, "account");
    

    Read more