Thursday, June 3, 2010

Filtered Lookup in CRM 4.0

How to create Filtered lookup in CRM 4.0

First of all you need to remote login to that server where CRm hosted after that go IIS root "C:\inetpub\wwwroot\_controls\lookup" then open the "lookupsingle.aspx" file and pase the below code in this 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 )
{
if (crmGrid.Parameters["search"] != null && crmGrid.Parameters["search"].StartsWith ("<fetch>"))
{
crmGrid.Parameters.Add("fetchxml", crmGrid.Parameters["search"]);
crmGrid.Parameters.Remove("searchvalue");
this._showNewButton = false;
}
}

</script>

For the main look up onchange event write below code

crmForm.all.ias_cityid.lookupbrowse = 1;
crmForm.all.ias_cityid.AddParam("search", "<fetch mapping="logical"><entity name="ias_cityEntity"><all-attributes><filter type="and"><condition value="'" operator="eq" attribute="ias_countryid"><condition operator="in" attribute="statecode"><value>0</value></condition></condition></filter></ALL-ATTRIBUTES>");</entity></fetch>

//remove the child lookup value
crmForm.all.ias_cityid.DataValue=null; // child lookup

// ias_cityEntity -- child lookup entity
// ias_country_id -- main lookup id
// ias_countryid -- main lookup attribute in sub entity
// ias_cityid -- sub lookup entity

Amma Bhagavan's Songs