Filtering Lookup based on the link entity

Introduction
Recently we got one 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.
blog1

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:

1. To create custom view we need things first
Unique view Id: We need this to setup for view, we can generate a new GUID using visual studio (Tools->Create GUID).

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.
blog2
2. Once we have these details we can combine them like following method and use it with addCustomView

function AddCustomContactView(executionContext) {
            var formContext = executionContext.getFormContext();
            if(formContext.getAttribute("account").getValue()!=null)
            {
            var accountId = formContext.getAttribute("account").getValue()[0].id;
            var viewId = "34A611CD-8503-4DE0-8EB7-B16EEAB32EBF";
            var entity = "contact";
            var ViewDisplayName = "Contacts";
            var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                "<entity name = 'contact' >" +
                "<attribute name='fullname' />" +
                "<attribute name='telephone1' />" +
                "<order attribute='fullname' descending='false' />" +
                "<link-entity name='account' from='accountid' to='parentcustomerid' link-type='inner' alias='ab'>" +
                "<filter type='and'><filter type='or'>" +
                "<condition attribute='accountid' operator='eq' uitype='account' value='" + accountId + "' />" +
                "<condition attribute='accountid' operator='under' uitype='account' value='" + accountId + "' />" +
                "</filter></filter></link-entity></entity></fetch >";
            var layout = "<grid name='resultset' jump='fullname' select='1' icon='1' preview='1'>" +
                "<row name = 'result' id = 'contactid' >" +
                "<cell name='fullname' width='300' />" +
                "<cell name='telephone1' width='125' />" +
                "<cell name='emailaddress1' width='150' />" +
                "</row></grid>";

            formContext.getControl("contact").addCustomView(viewId, entity, ViewDisplayName, fetchXML, layout, true);

        }
}


3. 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 !!

3 thoughts on “Filtering Lookup based on the link entity

  1. wajih2020

    Hi Mahender,
    very informative, I was just wondering that can it be used for customer lookup as well? because it seems that addCustomFilter() can’t have linked entity. any suggestion

    Reply
  2. Manasa

    Hi Mahender,

    I tried using the addCustomFilter() with Link entity and it didn’t work for me. Do you have any example that checks against Link entity as the addCustomFilter() doesn’t work for Link entities?I also tried placing the link entity in a different function and added only the node in the fetchXML but still I get the “No records found” error.

    Thanks,
    Manu.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *