Friday, July 17, 2015

Filtered lookup for Dynamics CRM 2013

Filtered lookup is a feature which was introduced in Dynamics CRM 2011, a greatly appreciated improvement from CRM 4. Prior to this, developers had to use third party tools to achieve the same result.
So, let’s start at the very beginning

What is filtered lookup?

When you have a large data set, and you want to filter the data in a progressive manner, then you need filtered lookup. You select one criteria/field and the data is filtered as per that. Now, if you choose the next field, then this filter is further applied to the already filtered data so as to give your further specific results.
For example: You have a list of prospective sales leads, and you want to filter out prior to a campaign based on those who are doctors and those who are in the state of Massachusetts. For this data selection, you would require a filtered lookup, so that the state/ profession is first selected and then following which on choosing the second criteria, the filter should be applied to the already once filtered data.

Now, let us write the code to achieve this.
This piece of code is very small as compared to what we used to write to achieve the same result in CRM 2011.

function preFilterLookup() {
debugger;
Xrm.Page.getControl("new_linkedaccount").addPreSearch(function () {
addLookupFilter();
});
}
function addLookupFilter() {
var fetchXml = '';
Xrm.Page.getControl("new_linkedaccount").addCustomFilter(fetchXml);
}
 
NOTE:
1. addLookupFilter function can only be called from addPreSearch function that binds the function to the Lookup Control. Although, you can provide any filter condition in the fetchxml form (which you can get using advanced find) to see the filtered records.
2. Two parameters ‘filter’ and ‘entityLogicalName’ are what the addCustomFilter method will accept. If the entityLogicalName is provided (not mandatory), the filter will only be applied to that entity type, else it will be applied to all types of entities returned.

For CRM 2013 Using addCustomFilter() to get Filtered Lookup Field based on Linked Entity

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...