"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу
Showing posts with label Lookup Filtering in CRM 2011 using Javscript OR Custom Lookup Filtering in CRM 2011 using Javascript. Show all posts
Showing posts with label Lookup Filtering in CRM 2011 using Javscript OR Custom Lookup Filtering in CRM 2011 using Javascript. Show all posts

Friday, 13 May 2011

Lookup Filtering in CRM 2011 using Javscript OR Custom Lookup Filtering in CRM 2011 using Javascript

Related Post : http://crmdm.blogspot.com/2011/04/lookup-fitering-feature-in-crm-2011-or.html

Let's see how we could configure custom lookup filter in CRM. Consider the following scenario.
We have a field called General Practitioner on the Account entity. We would like to place this field as a lookup of contacts. But the lookup of contacts should be filtered based on the profession field of Contact entity. So the lookup would be a list of doctors whose profession='Doctor'.

The contact records without filtering:


Also the contact form is shown below.




The main steps for the lookup filtering are the following.

Step 1: Build FetchXML
Step 2: Build Grid Layout
Step 3: Attach the new view to the lookup
So lets do it.

Step 1: Build FetchXML
As the first step of filtering we need to have fetchxml. But we don't need to worry about creation of FetchXML. MS Dynamics CRM will do it for you. But we need to decide 2 things
1) View of the Lookup
2) Filtering criteria
So navigate to Advanced Find and search for the contact records whose profession = 'Doctor'
Then you would get the following result.


We have defined a custom view with limited number of fields as shown below.


If we go back to Advanced Find tab, we could see a button called Download Fetch XML


Download the fetchXML to any local folder. The fetch XML file is shown below.


Step 2 & 3 should done in directly in the Js file. So we should call js function at the form load of Account. For your convenience, I have pasted the contents of the js file with proper comments.

Note: Do not forget to reformat the contents of FetchXML file as shown in the code below.
Step2 is the process of building a layout for the Lookup which is commented in the code (Its always ideal to keep same fields in the view used with the fetchXML and lookup View(except Id field). Remember the step Decide view while doing fetchXML). Also the step3 which is attaching the filterd lookup view to the lookup field General Practitioner is commented in the code.

function FilteredLookup() {
//Filter Condition--> Show Contacts which has got a Profession value as 'Doctor'
//Part 1 -->build fetchxml
var viewId = "{a76b2c46-c28e-4e5e-9ddf-951b71202c9d}"; //Any Guid is fine.
var entityName = "contact";// Entity to be filtered
var viewDisplayName = "Doctors"; // Custom name for the lookup window.
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='contact'>" +
"<attribute name='fullname' />"+
"<attribute name='contactid' />"+
"<attribute name='salutation' />"+
"<attribute name='new_profession' />"+
"<order attribute='fullname' descending='false' />"+
"<filter type='and'>"+
"<condition attribute='new_profession' operator='eq' value='100000001' />"+
"</filter>"+
"</entity>"+
"</fetch>";
// Part 2 --> Build Grid Layout. In other words, building a custom view for the Lookup
//building grid layout
var layoutXml = "<grid name='resultset' " +
"object='1' " +
"jump='name' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"<row name='result' " +
"id='contactid'>" + // Id attribute of the entity to be filtered
"<cell name='salutation' " + //Column1 - Salutation
"width='300' />" +
"<cell name='fullname' " + //Column 2 - Fullname
"width='100' />" +
"<cell name='new_profession' " + //Column 3 - Profession
"width='100' />" +
"</row>" +
"</grid>";
 

 
//Part3 add new view to the lookup
Xrm.Page.getControl("new_generalpractitioner").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
 

}


So here we go. As you could see only Doctors are listed in the lookup. The important point is that you could appy any filtering in the form of FetchXML. So I would say fetchXML is a cool feature in MS Dynamics CRM.