Unsupported Method for Filtered Lookup in MSCRM 4.0

Word of warning before proceeding – this is a unsupported customisation, it involves a change to one of the core MSCRM ASPX files and so cannot be guaranteed to continue functioning when Rollups or Products Upgrades are applied.

NOTE: this is a companion piece to a post on this blog for a Supported method of implementing a Filtered Lookup as a Dropdown, please take a look at the following link for further details.

To implement Filtered Lookups on a MSCRM 4 server, you need to edit the following file in the MSCRM Website:

\_controls\lookup\lookupsingle.aspx

So if your MSCRM Website is saved in the default location, this file would be located at C:\Program Files\Microsoft Dynamics CRM\CRMWeb\_controls\lookup.

Once opened, you will need to add the following script into the HTML of this existing ASPX page:

<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 )
{
      crmGrid.AddParameter( "fetchxml" , Request["search"] );
      crmGrid.Parameters.Remove( "searchvalue" );
}
</script>
Adding the Javascript Script Tag into the LookupSingle.aspx MSCRM Page

Adding the Javascript Script Tag into the LookupSingle.aspx MSCRM Page

As we are editing one of the core MSCRM Code files here, we can see why this is heavily unsupported!

NOTE: if your deployment of MSCRM is installed across multiple servers, this will need to done on each server.

Once this edit to the ASPX page is in place, you can now pipe a set of FetchXML to a Lookup field in MSCRM to filtered the possible selections for this Lookup field. In the following example, placing the script on the onLoad event of the Account entity to affect a Filtered Lookup for the Primary Contact:

FilterLookup = function(filteredLookup, entityTypeName, filter) {
    try {
        var fetchXML;

        filteredLookup.lookupbrowse = 1;
        
        fetchXML = "<fetch mapping='logical'>" +
                    "<entity name='" + entityTypeName + "'>" +
                    "<all-attributes/>" + filter + "</entity></fetch>";

        filteredLookup.additionalparams = "search=" + fetchXML;
    }
    catch (ex) {
        alert("FilterLookup - " + ex.description);
    }
}

try {
    // Main
    if (crmForm.FormType == 2) {
        var filterCondition = "<filter type='and'><condition attribute='parentcustomerid' operator='eq' value='" + crmForm.ObjectId + "'/></filter>";

        FilterLookup(crmForm.all.primarycontactid, "contact", filterCondition);
    }
}
catch (ex) {
    alert("Main - " + ex.description + ".");
}

This then results in the Primary Contact lookup being filtered to only Contacts that are associated with the Account:

MSCRM showing Lookup control filtered for only Contacts associated for an Account

MSCRM showing Lookup control filtered for only Contacts associated for an Account

We can similarly use this script to filter out selecting Accounts that are flagged as Suppliers when picking out the Supplier Account for an Order.

FilterLookup = function(filteredLookup, entityTypeName, filter) {
    try {
        var fetchXML;

        filteredLookup.lookupbrowse = 1;

        fetchXML = "<fetch mapping='logical'>" +
                    "<entity name='" + entityTypeName + "'>" +
                    "<all-attributes/>" + filter + "</entity></fetch>";

        filteredLookup.additionalparams = "search=" + fetchXML;
    }
    catch (ex) {
        alert("FilterLookup - " + ex.description);
    }
}

try {
    // Main
    var filterCondition = "<filter type='and'>" +
                          "<condition attribute='customertypecode' operator='eq' value='10'/>" +
                          "<condition attribute='statecode' operator='eq' value='0'/>" +
                          "</filter>";
    FilterLookup(crmForm.all.new_supplieraccountid, "account", filterCondition);
}
catch (ex) {
    alert("Main - " + ex.description + ".");
}
Selecting a Supplier Account for an Order

Selecting a Supplier Account for an Order

Selecting a Supplier Account for an Order via a Filtered Lookup only showing Accounts that are flagged with a Relationship Type of Supplier

Selecting a Supplier Account for an Order via a Filtered Lookup only showing Accounts that are flagged with a Relationship Type of Supplier

Whilst this ‘hack’ does operate without issue as per MSCRM v4.0 Rollup 11, this is unsupported and so should only ever be used with caution.

As a side-note, this scripting does not alter the behaviour of the Form Assistant which continues to present the full unfiltered list of Lookup options.

This will be standard functionality in MSCRM v5 to place Filtered Lookups on form without resorting to Scripting.

2 Responses to Unsupported Method for Filtered Lookup in MSCRM 4.0

  1. Pingback: Filtered Lookup as a Drop-down Picklist in MSCRM | CRM Consultancy Blog

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

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