CRM 2011: Overriding lookup onclick event

I know that some of you can’t wait to know how to override lookup control onclick event. Indeed, Microsoft has heavily used htc file for their controls behavior and it’s more difficult to change these behaviors than it was in Dynamics CRM 4.0…

So, here is the solution:

var element = document.getElementById("lookup_attribute_logicalname");
element.onshowdialog = function (event) {
var url = "http://you_custom_page_url" ;

var result = window.showModalDialog(url);
event.oLookupItems = { items: result };

The object returned from the page should be an array of Lookup items, like the following (example for an account):

var lookupItems = new Array();
lookupItems[0] = new Object();
lookupItems[0].id = "{...}"
lookupItems[0].name = "My account";
lookupItems[0].type = 1;
lookupItems[0].entityType = "account";

window.returnValue = lookupItems;

To be honest, I’m not the one who found this solution. All the credit goes to my colleague Nicolas.


Atul said…
Hi Tanguy,
First of all thanks a lot for providing these use functionality.

I have 1 query, i am able to open my custom url, but after selecting a record from that opened custom window i'm not able to set the selected record in to lookup field. can you tell me how we set selected record in to the lookup field.

Thanks & Regards,
Tanguy said…
Hi Atul,

I can't give you more information than what is defined in the blog post...

Maybe you could paste in the comments the parts of your code that use the feature...
Joy said…
Hi, I use your code, but there is a little issue: After I select one record, it pop up the lookup view window again.

Do you have any idea?
Tanguy said…
Yes, I found it too...

It seems to occur if you use a non overriden lookup first.

If you start by using the overriden lookup, the problem does not occur.

I had a solution but need to retrieve it... The solution was to simulate a click on the overriden lookup during OnLoad to be sure the problem won't occur
kh_heckfy said…
You can throw the code that solves this problem?
I do not understand the last comment on this article
Anonymous said…
Hi Tanguy,

Looks pretty simple, and I can see its working for some of you.

But for me its not working when I am trying on CRM 2011 rollup 18, my requirement is simple to ignore standard dialog box on some condition and show alert message and in other condition to show it as regular.

Please let me know if you have any idea why its not working on my side.

Best Regrd,
Youssef ESSOUFY said…
Hi Said,
For CRM 2011 UR 18, try using onclick event instead of onshowdialog, it works for me.
here is the code i use:

function initLookup(attributeName){
var element = document.getElementById(attributeName);
element.onclick = function (event) {
var url = "";
var result = window.showModalDialog(url);
if (result != undefined) {

Best Regrd,

Popular posts from this blog

New tool: CrmDiagTool 2011

New tool: SiteMap Editor for Microsoft Dynamics CRM 2011

Full featured CRM grid in HTML/JS