Filtering multiple custom CRM Lookup

Let me explain the scenario..

We have a Model lookup and a variant Lookup on Lead form. Every variant is associated with a particular model. i.e One model have multiple Variant.

Eg.
Model          Varient
Alto             Alto LX
Alto             Alto Xcite
Alto             Alto LXI
SX4               SX4 VXI
SX4               SX4 ZXI Leather
SX4               SX4 ZXI
—————————-

Now Requirement is that when user select a model from Model lookup then only variant corresponding to selected model should be displayed to user when user click on variant lookup and disable the search in Variant lookup.

Write below code on Model OnChange event.

if(crmForm.all.new_modelid.DataValue != null )
{
var modelLookup=crmForm.all.new_modelid.DataValue;
var modelID=modelLookup[0].id;
var modelName=modelLookup[0].name;
var field=crmForm.all.new_variantid;

// Pass fetch xml through search value parameter
field.lookupbrowse = 1;
field.AddParam(“search”,”<fetch mapping=’logical’ distinct=’false’>”
+”<entity name=’new_varientmaster’>”
+”<attribute name=’new_varientmasterid’/>”
+”<attribute name=’new_name’/><attribute name=’createdon’/>”
+”<order attribute=’new_name’ descending=’false’/><filter type=’and’>”
+”<condition attribute=’new_modelnameid’ operator=’eq’ uiname='”+ modelName
+”‘ uitype=’new_modelmaster’ ”
+”value='”+ modelID +”‘ />”
+”</filter></entity></fetch>”

);
}

Note: Put below code if not already exist

The following code needs to be inserted anywhere in the <CRM site folder>\_controls\lookup\lookupsingle.aspx file if not already exist (if you have already code for filtering lookup)..

<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”);

// and then select it.
this._showNewButton = false;
}
}

</script>

For details  about lookupsingle.aspx code visit below link.

http://crm.georged.id.au/post/2008/02/16/Filtering-lookup-data-in-CRM-4.aspx

For Reference: Model is custom entity (new_modelmaster) which contains name attribute (i.e Model Name) and Variant is custom entity (new_variantmaster) which contain name attribute( variant Name ) and Model lookup(new_modelnameid) to map model with variant.

For building FetchXml Query find below link.

https://arvindcsit.wordpress.com/2010/09/08/using-the-advanced-find-for-fetchxml-builder/

Enjoy J

Author: Arvind Singh

Solution Architect with 15+ years of exp. Dynamics CRM, Power Platform, Azure which includes Solution, Design, Development, Deployment, Maintenance and support experience.

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: