Filtered Lookup in CRM 2011

Posted: May 22, 2012 in CRM 2011, Jscripts

Filtered Lookup using customizations in CRM 2011

We can filter the records by using crm 2011 out of box functionality by setting up the properties for your lookup.

For example you have a custom entity (Industries) which has an Account and Contact lookup on your form. Once an Account record is selected, you need to show only Related contacts for that particular Account record.

Add the Filtering property to your custom contact lookup  as shown below.

After adding the filter property, you can see the contact records getting filtered as shown in the below screen shot.

Filtered Lookups via scripting in CRM 2011

By using addCustomView XRM method, you can get the entire new custom view for the Lookup.

function onload() {
   var accountId;
   // get the account id
   accountId = Xrm.Page.getAttribute("crm_accountid").getValue() != null ? Xrm.Page.getAttribute("crm_accountid").getValue()[0].id : null;
   if (accountId == null) {
      // set the empty GUID
      accountId = "00000000-0000-0000-0000-000000000000";
   }
   setContactLookup("crm_contactid", accountId);
}

// onchange event for the account lookup
function account_onchange() {
   // Set the Contact Lookup null 
   Xrm.Page.getAttribute("crm_contactid").setValue(null);
   var accountId;
   // get the account id
   accountId = Xrm.Page.getAttribute("crm_accountid").getValue() != null ? Xrm.Page.getAttribute("crm_accountid").getValue()[0].id : null;
   if (accountId == null) {
      // set the empty GUID
      accountId = "00000000-0000-0000-0000-000000000000";
   }
   setContactLookup("crm_contactid", accountId);
}

// filter the related contact records based on account
function setContactLookup(lookupFieldName, accountId) {
   // add the randomly generated GUID for the view id
   var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E3}";
   var entityName = "contact";
   // add the view display name
   var viewDisplayName = "Active Contacts";
   // fetch xml for filtered contact records
   var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" + 
                               "<entity name='contact'>" + "<attribute name='fullname' />" + 
                                  "<attribute name='contactid' />" + 
                                  "<order attribute='fullname' descending='false' />" + 
                                  "<filter type='and'>" + 
                                      "<condition attribute='parentcustomerid' operator='eq' value='" + accountId + "' />" + 
                                  "</filter>" + 
                               "</entity>" +
                            "</fetch>";
   // Grid Layout
   var layoutXml = "<grid name='resultset' " + 
   "object='1' " + 
   "jump='contactid' " + 
   "select='1' " + 
   "icon='1' " + 
   "preview='1'>" + 
   "<row name='result' " + 
   "id='contactid'>" + 
   "<cell name='fullname' " + 
   "width='200' />" + 
   "</row>" + 
   "</grid>";
   // add the custom view for the lookup field
   Xrm.Page.getControl(lookupFieldName).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);

   // disable the view picker and new button to restrict creating or adding non filtered contact records records
   eval("crmForm.all." + lookupFieldName + ".AddParam('ShowNewButton', '0');");
   eval("crmForm.all." + lookupFieldName + ".AddParam('DisableViewPicker', '1');");
}

Below is the screen shot which disabled the view picker and hides the New button.

Advertisements
Comments
  1. Muhammad Azeem Nawaz says:

    great article. what a simple solution for filtered view! Thanks 🙂

  2. Jeff says:

    2nd time one of your posts has really helped me out! thanks man!

Leave a Reply

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