Locking Down Lookup Fields
I've encountered many situations during a CRM Deployment where we want to restrict the type of record that can be selected in a Lookup Field. In most cases it's been situations dealing with Opportunities and Cases in CRM. The Potential Customer & Customer fields can accept either an Account or a Contact as a value. What if we only want users to select Accounts? The Javascript below gives an example of how you can force users to only be able to select an Account when creating a new case in CRM.
When a user triggers the OnChange() event for the Customer Lookup field on a Case the JavaScript checks to make sure it has an Object Type value of 1, which is the Object Type Code for an Account. If it isn't an Account an alert message appears and the lookup field is cleared out.
try
{
if(crmForm.all.customerid.DataValue[0].type != 1)
{
window.alert("You must choose an Account for the Customer Field.");
crmForm.all.customerid.DataValue = null;
}
catch(err)
{
//window.alert("Display Error Message Here");
}
If you wanted to change this code so that only Contacts could be selected, then you would check for a Object Type Code of 2, which stands for a Contact. It is some short and simple JavaScript that will help control how information is recorded in your CRM system.