how to add custom filter method for lookup control in crm

5
How to Add Custom Filter Method for Lookup Control in CRM 1). Filter Lookup: In Microsoft Dynamics CRM there are some examples or solutions which are provided for filtered lookup in CRM, there are some different persons which have given solutions for filtered lookup in CRM a solution for filtered lookup in CRM4, but it requires you to modify CRM application files, which I usually try to avoid unless absolutely no choice, as it will cause deployment and maintenance issue. Here solution is given. The following are the steps that have been taken 1. Create a subfolder called CustomLookup under your CRM's ISV folder. 2. Copy <crmweb folder>\_controls\lookup\lookupsingle.aspx page to your ISV\CustomLookup folder which you have just created. 3. Add the following script tag to <crmweb folder>\ISV\ CustomLookup\lookupsingle.aspx (Please thank George for his code, this is basically a copy from his blog). <%--************ BEGIN: Custom Changes ************-- %> This page is a copy of <crmweb folder>\_controls\lookup\lookupsingle.aspx file with the following custom change. --%> <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 )

Upload: aegis-canada

Post on 24-Jun-2015

222 views

Category:

Business


2 download

DESCRIPTION

In Microsoft Dynamics CRM there are some examples or solutions which are provided for filtered lookup in CRM. Visit Us to Know More Informationhttp://goo.gl/ww2HFR

TRANSCRIPT

Page 1: How to Add Custom Filter Method for Lookup Control in CRM

How to Add Custom Filter Method for Lookup Control in CRM

1). Filter Lookup:

In Microsoft Dynamics CRM there are some examples or solutions which are provided for filtered lookup in CRM, there are some different persons which have given solutions for filtered lookup in CRM a solution for filtered lookup in CRM4, but it requires you to modify CRM application files, which I usually try to avoid unless absolutely no choice, as it will cause deployment and maintenance issue.

Here solution is given. The following are the steps that have been taken

1. Create a subfolder called CustomLookup under your CRM's ISV folder.2. Copy <crmweb folder>\_controls\lookup\lookupsingle.aspx page to

your ISV\CustomLookup folder which you have just created.3. Add the following script tag to <crmweb folder>\ISV\CustomLookup\

lookupsingle.aspx (Please thank George for his code, this is basically a copy from his blog).

<%--************ BEGIN: Custom Changes ************--%>

This page is a copy of <crmweb folder>\_controls\lookup\lookupsingle.aspx file with the following custom change. --%>

<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>

4.Add the following script to your form’s no-load event

Page 2: How to Add Custom Filter Method for Lookup Control in CRM

(function ReplaceCrmLookup(){  window.oldOpenStdDlg = window.openStdDlg;  window.openStdDlg = function() {  arguments[0]=arguments[0].replace(/\/_controls\/lookup\/lookupsingle.aspx/i, '/ISV/CustomLookup/lookupsingle.aspx');  return oldOpenStdDlg.apply(this, arguments);    };});

5. Then you are ready to add filtering criteria to your lookup field. For instance, there is a business requirement that you need to do filtering for the Primary Contact field of Account form, so that when the Primary Contact lookup is clicked, the lookup window will only show the contact records that are associated to the current account. Your code will be like this:

(function AddFilterToPrimaryContactEntity(){    var lookup = crmForm.all.primarycontactid;    if(crmForm.ObjectId == null){        field.Disabled = true;    }    else{        lookup.lookupbrowse = 1;// Pass fetch xml through search value parameter        lookup.AddParam('search','<fetch mapping="logical">'+'<entity name="contact">' +   '<filter>'+'<condition attribute="parentcustomerid" operator="eq" value="' + crmForm.ObjectId + '" />' +'</filter>' + '</entity>' +'</fetch>');    }})();

6.  Save your form and publish your entity, you are good to go now.

Note :- that when you implement a filtered lookup field using the script here, you need to check "Turn off automatic resolutions in field" option in the CRM lookup field's property window; otherwise the lookup dialog page will throw an error when the user has typed in something in the lookup textbox. The option is unchecked by default.

 

2) How to clear Lookup in CRM 2011/ Lookup filtering and clearing lookup in CRM 2011 / How to pass argument to .js function in CRM 2011:

Page 3: How to Add Custom Filter Method for Lookup Control in CRM

 

As we have known, one of the best features of CRM is lookup filtering just because it reduces our coding. We consider that filtering second lookup based on first one. We need to clear the second lookup whenever the user changes the selection of the first lookup.

Here we have a Scenario:

2 lookups Country and City

When the user selects UK from first lookup then second lookup lists cities belongs to UK (based on lookup filtering feature). Now the user would like to update Country to Denmark. So when the user selects country as Denmark then the city lookup should be cleared. And then the user gets a chance to choose cities belongs to Denmark.

Code:function SetLookupNull(lookup Attribute){var lookup Object = Xrm.Page.getAttribute(lookupAttribute);

if (lookupObject != null) { Xrm.Page.getAttribute(lookupAttribute).setValue(null);}}

The function call is on the onchange event of first lookup

Please note that we need to pass the attribute name of the lookup which should be cleared.Please note that we need to pass the attribute name of the lookup which should be cleared.

Page 4: How to Add Custom Filter Method for Lookup Control in CRM

The key advantage of the above function is reusability. You could clear any lookup value as per the need by supplying the attribute name as shown.

Follow US

aegissoftware [email protected] [email protected]

Website: http://www.aegissoftwares.com/