Filtered Lookup in CRM 2011

To get the filtered lookup functionality in CRM 2011 we can make use of the

addCustomView Method of Xrm.Page.ui control.


Something similar to this

Here we have a custom entity name Case which has 1-n relationship with Contact Entity.

Now we want to show only those contact records in lookup which are associated with a particular case record.

We can add the code to the onload of the Entity.

// View ID --> Generate and Assign a new guid.

var viewId = "{C0F1DD64-1BF3-450D-BCDE-DF4732DE1606}";

// Set the entity name
 var entityName = "contact";
 // Give a meaningful name to the custom view
 var viewDisplayName = "Case Member View";

// Create the Advanced find query and download the fetch xml
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='contact'>" +
"<attribute name='fullname' />" +
" <attribute name='contactid' />" +
"<order attribute='fullname' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='new_caseid' operator='eq' uiname='new case' uitype='new_case' value="+caseID+"/>" +
"</filter>" +
"</entity>" +
"</fetch>";

// specify the layout for the results and which field to display
var layoutXml = "<grid name='resultset' " +
"object='1' " +
"jump='name' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"<row name='result' " +
"id='contactid'>" +
"<cell name='fullname' " +
"width='100' />" +
"</row>" +
"</grid>";
// specify the schemaname of the lookup control
var lookupControl = Xrm.Page.ui.controls.get('new_clientid');
// set the parameters

lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);

13 thoughts on “Filtered Lookup in CRM 2011

  1. I tried the code mentioned above & it does work.However, it does not seem to be sorting (ordering) the records in the lookup control.Do you also recognize this behaviour ?

  2. Pingback: Filtered Lookups in CRM 2011 | CRM Consultancy Blog

  3. Very Good and Easy to follow,

    But how did you get the GUID??. I have different views i need to create custom filters for but they cant all be using the same GUID??

    cheers

  4. Very useful.
    Do you know why It is not run with standard product lookup in order product ?
    Have you a workaround ?

    Thanks
    Massimo

  5. I am a CRM developer and I found your information very useful. Thanks a lot for your time to post such useful blogs.

    Keep Writing Best of Luck .

  6. Pingback: Setting Custom View as default for lookup in Connection entity. (CRM 2011) « Nishant Rana's Weblog

    • Hey Nishant, Thanks for great info…

      Can you please tell me, How i can write lookupbrowse in CRM 2011

      Below code is in CRM 4.0… i need same in crm 2011..
      var field = Xrm.Page.getAttribute(“attributename”).getValue();
      field.lookupbrowse = 1;

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s