+ Start a Discussion

Making a custom lookup field required

I have a custom object with a Contact lookup field. Is it possible to make this field required? It would need to be required through both the API and the Salesforce.com interface. Is this only possible with a trigger?





I was surprised to discover that you can't make a lookup field universally required, like you can for text fields, for example, when creating the custom field. 


For a lookup, I think you'll want to do a combination of things.  First, for users entering data through the UI, you should make the field required on the page layout.  Then, for the API, you should create a validation rule on your custom object that fires when someone tries to save and your lookup field is blank.


Me too!


I ended up going with a simple trigger. It works in the web interface as well as through the API.



trigger requiredContactTrigger on Ice_Cream_Survey__c (before insert, before update) { for (Ice_Cream_Survey__c i: Trigger.new) { if (i.Contact__c == NULL) { i.Contact__c.addError('Contact is required'); } } }




Manish S.ax896Manish S.ax896



You can also do this using a simple validation rule on your custom object Ice_Cream_Survey__c

ISBLANK( Contact__c )


With Regards