Filtering lookup data in CRM 4

Posted on February 16, 2008 04:11 by George Doubinski

Now we all know that when Microsoft says "unsupported" they always sometimes mean business. Take, for example, popular CRM 3 customisation to filter lookup field data that used to work well. Until version 4, that is. Not all is lost, however, because no thanks to me but to Adi Katz, now there is new unsupported way to make it work in CRM 4 as well.

For example, to make primary contact lookup for an account record to show contacts from this account only, the following customizations are required:

Form OnLoad script

The script looks almost identical to the original one except for one very important change. We have to pass fetch xml string using one of the parameters recognised by the server otherwise exception is thrown (there is a way to disable parameters check through the registry setting DisableParameterFilter, but it's easier without yet another undocumented setting). Since we want to disable search functionality it makes sense to re-use search parameter.

var field = crmForm.all.primarycontactid;
if(crmForm.ObjectId == null)
{
    // Disable lookup for new account record as there can be no contacts
    field.Disabled = true;
}
else
{
    // Ensure that search box is not visible in a lookup dialog
    field.lookupbrowse = 1;
   
    // Pass fetch xml through search value parameter
    field.AddParam("search",
     "<fetch mapping='logical'><entity name='contact'>"
    + "<filter><condition attribute='parentcustomerid' operator='eq' value='"
    + crmForm.ObjectId
    + "'
/></filter></entity></fetch>");
}

Field properties

If automatic resolution for the field is enabled and user types something in the field, it causes direct web service call that would ignore our fetch xml. In other words, user would be able to set the field value to any of the contacts in the database by simply typing contact's name. Disabling automatic resolution solves the issue:

Field properties dialog

Lookup dialog

Turns out, grid control in version 4 still recognises fetch xml, the challenge was to pass it. Kudos to Adi for finding a very clever workaround to inject fetch xml directly. The following code needs to be inserted anywhere in the <CRM site folder>\_controls\lookup\lookupsingle.aspx file.

<script runat="server">

protected override void OnLoad( EventArgs e )
{
      base.OnLoad(e);
      crmGrid.PreRender += new EventHandler( crmgrid_PreRender );
}

void crmgrid_PreRender( object sender , EventArgs e )
{
    // As we don't want to break any other lookups, ensure that we use workaround only if
    // search parameter set to fetch xml.
    if (crmGrid.Parameters["search"] != null && crmGrid.Parameters["search"].StartsWith("<fetch"))
    {
        crmGrid.Parameters.Add("fetchxml", crmGrid.Parameters["search"]); 

        // searchvalue needs to be removed as it's typically set to a wildcard '*'
        crmGrid.Parameters.Remove("searchvalue"); 

        // Icing on a cake - ensure that user cannot create new contact outside of the account
        // and then select it.
        this._showNewButton = false;
    }
}

</script>

 

You are on your own

I cannot stress enough that this customisation is 100% unsupported so use it at your own peril. In particular, because server files were modified, there is a good chance that you'll see yellow screen of death on a regular basis (if you've switched DevErrors on, that is). Much easier, supported way to achieve the same functionality (and much more) is to use custom lookup dialog from Michael Höhne.

Happy hacking!

Currently rated 4.5 by 53 people

  • Currently 4.471695/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

April 20. 2008 08:22

pingback

Pingback from mscrm-community.de

Juergen Beck's Blog about MS CRM and other things... : Endlich wieder da: Gefilterte Lookups mit MSCRM 4.0

mscrm-community.de

January 18. 2009 04:47

pingback

Pingback from hilpers.com

nur Produkte der Angebots-Preisliste anzeigen | hilpers

hilpers.com

March 20. 2009 10:00

pingback

Pingback from sivavadavalli.wordpress.com

Filtered Lookup Work Around in CRM 4.0 « Siva Vadavalli Weblog

sivavadavalli.wordpress.com

June 8. 2009 18:40

pingback

Pingback from nishantrana.wordpress.com

Filtered Lookup CRM 4 « Nishant Rana's Weblog

nishantrana.wordpress.com

June 12. 2009 14:07

pingback

Pingback from keyongtech.com

Displaying contacts related to accounts only while looking up reco | keyongtech

keyongtech.com

May 2. 2010 10:06

pingback

Pingback from mayankp.wordpress.com

Filtering Lookup Data in CRM 4.0 « MayankP's Blog

mayankp.wordpress.com

May 6. 2010 16:49

pingback

Pingback from mahenderpal.wordpress.com

Filtered Lookup in MS CRM 4.0 « Mahender Pal

mahenderpal.wordpress.com

May 21. 2010 03:58

pingback

Pingback from 424.an74.com

G1500 Sample S15 Jimmy, S15 Headlight Oem Nissan Jdm

424.an74.com

Comments are closed