We all have known that in CRM 2013 we can filter the lookup field using easier method compared to CRM 2011, that is using addCustomFilter() combined with addPreSearch() function.

This is one of the example how to use it, to filter contact based on email that contains @example.com

function onLoad()
{
addEventHandler();
}

function addEventHandler() {
// add the event handler for PreSearch Event
Xrm.Page.getControl("parentcontactid").addPreSearch(addFilter);
}

function addFilter() {
//find contact contains this @example.com
var email = "%@example.com%";

//create a filter xml
var filter = "" +
"" +
"
";

//add filter
Xrm.Page.getControl("parentcontactid").addCustomFilter(filter);
}

Call the onLoad function during form onLoad event.

That code is looks good and it will work, yes, it will.

And if you notice, this is a very simple way, compared to the old code, using addCustomView(), which you need to build your own custom view and don’t forget to build layoutXML as well.

This is the addCustomView().

Xrm.Page.getControl(arg).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault)

However, there is a limitation, not only that is because those functions can only work for lookup field in the Refreshed UI Entity Form (it means cannot work for Price List Item, Quote Product, etc.), but this filter, if you notice, it can only work to filter using the attribute that is a part of that entity. So, it cannot be used to filter an entity based on the field from another entity.

The above example is to filter contact by email address (email address is one of the contact attribute), what if we are going to filter an entity record based on another related entity, for example in the Lead form, it has Existing Contact lookup field, then the users want to filter to show only Contact based on the Account Number or based on the Website/Email Address of Account or based on the Account Name that compared to the obtained Company Name in Lead form .

If you notice, the filter that is supported is currently just using the node that is started from ..

//create a filter xml
var filter = "" +
"" +
"
";

However, from that XML we get (see the example below), if we are using another related entity as the filter, it is using , which is not allowed in the addCustomFilter() method.

This is the example of the generated XML with criteria based on the another related entity’s field:

*The XML to get Contact based on Account’s email address.
.















So…

We cannot use these lines as variable filter.






As we know that the only way is still using the old-fashioned CRM 2011 Code, that is using addCustomView() with many parameters that you might not want to build anymore.

Xrm.Page.getControl(arg).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault)

So, we should build our own fetchXML + layoutXML as well, in fact you have built your own view and you just need to filter it.

Then, as the alternative, you can use another code to find that related entity id, see this example:

var filter = "" +
"" +
"
";

//add filter
Xrm.Page.getControl("parentcontactid").addCustomFilter(filter);

You can use OData Query to find the Account Id.

Another limitation in addCustomFilter() is it only supports And
(based on this link:http://msdn.microsoft.com/en-us/library/gg334266.aspx#BKMK_addCustomFilter)

If you need more than one Account Id, then you might need to add Operator IN, see this example:



{DBBB8E56-B9D9-E311-9410-001CC4EECDD6}
{DDBB8E56-B9D9-E311-9410-001CC4EECDD6}
{DFBB8E56-B9D9-E311-9410-001CC4EECDD6}


You might choose whether you want to use addCustomView() with those many required parameters or addCustomFilter() with that limitation and have to find the ID, but with no need to pass many parameters as the addCustomView() required.

Summary:

1. To use addCustomFilter() to filter based on the same entity field:

function addFilter() {
//find contact contains this @example.com
var email = "%@example.com%";

//create a filter xml
var filter = "" +
"" +
"
";

//add filter
Xrm.Page.getControl("parentcontactid").addCustomFilter(filter);
}

2. To filter based on the related entity (one record only):

 //create a filter xml
var filter =
"" +
"" +
"
";

3. To filter based on the related entity (multiple records)

//create a filter xml 
var filter =
"" +
"" +
"{DBBB8E56-B9D9-E311-9410-001CC4EECDD6}" +
"{DDBB8E56-B9D9-E311-9410-001CC4EECDD6}" +
"{DFBB8E56-B9D9-E311-9410-001CC4EECDD6}" +
"
" +
"
";

Hope this helps!

Thanks.