Filtering Lookup Based On The Link Entity

Introduction


Recently, we got a request to filter lookup based on the account as well as parent account of the account. This blog post is about how we implemented this requirement.
 

Solution

 
If we want to filter lookup based on the other lookup we can simply edit target field properties and can set Related Record Filtering option. We can select our source lookup and select which relationship we want to use for filtering, for example in following screen you can see that contact lookup filtering is set based on the Company name lookup, so we will see contact associated to the contact selected under company name.
 
But some time we want to filter lookup based on some other condition, in that case we can use PreSearch methods of lookup filtering where we can pass our fetch filter condition and our lookup will be filtered based on the condition. To implement our requirement we first tried using the filter condition but it did not work so we tried third options to create a custom view based on our condition and set it as the lookup view so that user can see data based on the filter. To create custom view and pass it to the lookup we can use following steps.
 
To create custom view, we need the following things first:

Unique view Id
 
We need this to setup for view, we can generate a new GUID using visual studio (Tools->Create GUID).
  1. var viewId = “34A611CD-8503-4DE0-8EB7-B16EEAB32EBF”;  
ViewDisplayName
 
Display name of the view.

FetchXML
 
Valid fetchXML which we can create using Advanded Find and download our fetchXML.

Layout
 
We need a valid view layout xml, we can create it manually or we can simply copy it from existing view of that entity. We can create solution and can include our entity,export it and can copy layout XML from the view section.

Once we have these details we can combine them like following method and use it with addCustomView,
  1. function AddCustomContactView(executionContext) {  
  2.             var formContext = executionContext.getFormContext();  
  3.             if(formContext.getAttribute("account").getValue()!=null)  
  4.             {  
  5.             var accountId = formContext.getAttribute("account").getValue()[0].id;  
  6.             var viewId = "34A611CD-8503-4DE0-8EB7-B16EEAB32EBF";  
  7.             var entity = "contact";  
  8.             var ViewDisplayName = "Contacts";  
  9.             var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +  
  10.                 "<entity name = 'contact' >" +  
  11.                 "<attribute name='fullname' />" +  
  12.                 "<attribute name='telephone1' />" +  
  13.                 "<order attribute='fullname' descending='false' />" +  
  14.                 "<link-entity name='account' from='accountid' to='parentcustomerid' link-type='inner' alias='ab'>" +  
  15.                 "<filter type='and'><filter type='or'>" +  
  16.                 "<condition attribute='accountid' operator='eq' uitype='account' value='" + accountId + "' />" +  
  17.                 "<condition attribute='accountid' operator='under' uitype='account' value='" + accountId + "' />" +  
  18.                 "</filter></filter></link-entity></entity></fetch >";  
  19.             var layout = "<grid name='resultset' jump='fullname' select='1' icon='1' preview='1'>" +  
  20.                 "<row name = 'result' id = 'contactid' >" +  
  21.                 "<cell name='fullname' width='300' />" +  
  22.                 "<cell name='telephone1' width='125' />" +  
  23.                 "<cell name='emailaddress1' width='150' />" +  
  24.                 "</row></grid>";  
  25.    
  26.             formContext.getControl("contact").addCustomView(viewId, entity, ViewDisplayName, fetchXML, layout, true);  
  27.    
  28.         }  
  29. }  
Finally, we can use this code on change of the contact lookup as well as onload of the entity form and now we should be able to see contact from account as well as it’s parent account.
 
Hope it will help someone !!
Next Recommended Reading How to Create Custom Lookup View in CRM

HIMBAP
We are expert in Microsoft Power Platform.