Filtering lookup data in CRM 4

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

We have to pass fetch xml string using one of the parameters recognized 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:

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>

4 thoughts on “Filtering lookup data in CRM 4”

  1. Hi Do you know of any reason this would work in my DEV environment but not my Prod ?

    Are there perhaps any known issues or permissions settings that must be set? Ive basically set IE to allow everything and still no luck.

    Like

  2. Thanks a lot for the post. Before I implement this, I want to know if this is supported by Microsoft as it involves changes to lookupsingle.aspx.

    Like

  3. Thanks very much for the post and its working perfecly! I need to work around for another task that is user wil enter the value in lookup search box, instaead of default search it should use custom fetchxml .
    Its a singlelookup.aspx.

    Thanks in advance

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

%d bloggers like this: