Correcting available lookup views when restricting lookup types via JavaScript in CRM

Although unsupported, in CRM you can use JavaScript to restrict the available lookup types for a lookup field on the form. For example, on the Email form I’d like to restrict the Regarding field to be able to lookup Client and Incident records only. Below is the code that’d (partially) achieve this. Again, please note that this is unsupported.

//The code below is for CRM 2011. In CRM 2013 (and possibly 2015), 
//the ID for DOM elements that represent fields are suffixed with '_i'.
//Therefore, the ID below in CRM 2013 would be 'regardingobjectid_i'.

var regardingField = document.getElementById("regardingobjectid");
regardingField.setAttribute("lookuptypes", "2,112");

2 and 112 are the object type codes for Client and Incident. Below is the result of the above code.

1 Lookup Types

While this looks like it does the trick, something is wrong if you look closely. The list of available lookup views is not correct. It is still listing the available lookup views for the Account entity, not the Client entity. Note that this would only occur if you launch the lookup dialog, and the lookup field on the form does not currently have a value, e.g. when creating a brand new Email.

The lookup element has a number of attributes that control its behaviour. One of these is the defaulttype attribute, and it seems that the dialog is listing the available lookup views for the defaulttype by default. So to fix this, we need to set defaulttype to one of our allowable lookup types. This can be achieved with the additional line of code below.

regardingObject.setAttribute("defaulttype", "2");

Below is the result of this additional line.

2 Lookup Types

OK this looks better, but there is however another issue. In the dialog, if you change the lookup entity to something else (e.g. Incident), and then switch back to the default type (i.e. Client in this example), you will find that a lookup view is not selected by default. Furthermore, if you go ahead and pick a lookup view and select a record and click OK, then you will receive a JavaScript error from CRM.

To correct this issue, we need to set another attribute for the the lookup DOM element, namely defaultviewid. Below is the additional line of code.

regardingObject.setAttribute("defaultviewid", "");

So in conclusion, to avoid issues when restricting lookup types, the complete code you should use is as below.

var regardingObject = document.getElementById("regardingobjectid");
regardingObject.setAttribute("defaulttype", "2");
regardingObject.setAttribute("defaultviewid", "");
regardingObject.setAttribute("lookuptypes", "2,112");

This was observed on CRM 2011 with UR 18.

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in CRM. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s