//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.
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.
Below is the result of this additional line.
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.
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.