Jeremy Winchell

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.

Comments

vbullinger said:

That works, but what you could also do is this, in the onload event handler of the form (not the onchange of the attribute):

crmForm.all.customerid.lookuptypes = 1;

That way, when they open up the lookup dialog, it won't give them the option of selecting "2"s (contacts).

This is still very useful, in situational cases of maybe 2s being acceptable in some cases, but not others.  Or, maybe because they expect to be able to pick 2s, and would like an alert to tell them why picking a 2 is unacceptable, for example.

# October 4, 2007 11:34 AM

Jeremy Winchell said:

I did not know about that option.  That is much less work than the JavaScript I posted.  Thanks for the tip.

# October 5, 2007 9:21 AM

jmd30094 said:

I am currently using the onload evet code of

"crmForm.all.customerid.lookuptypes = 1;"  because I only want users to choose from Accounts.

Does anyone know how I could add a second criteria to this script to further limit the specific Accounts that the users can choose.  For example, I have a custom attribute that defines whether an Account is a Parent Account or a SubAccount.  I would like to restrict my users to only choosing Accounts that are tagged as SubAccounts.

# January 30, 2008 3:16 PM

Resimler said:

Thank you Jeremy Winchell.Good Point.

# April 14, 2008 10:23 AM

Jeremy Winchell said:

jmd30094,

In order to do that you would have implement a type of Lookup Filtering code, which is unsupported in CRM.  However, if you do go that route you can use the FetchXML syntax to further limit the records that can or can't be selected when using the Lookup.

Jeremy

# October 22, 2008 7:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)