+ Start a Discussion
MBrady325MBrady325 

Creating a formula to make sure lookup fields are unique

I have two custom objects, one of which points to another via a lookup field, and I want to create a validation rule to ensure that when one record of the second object type is pointed to in said field, another will be unable to point to that same record.  That is, if Object1 has a lookup field called "Object2Lookup", and if Record A of type Object1 points to Record X of  type Object2 through that record's Object2Lookup field, I want to make sure that Record B of type Object1 gets an error if it tries to point to Record X in its Object2Lookup field.  How can I create a validation rule (or lookup filter) to do this?

Best Answer chosen by Admin (Salesforce Developers) 
SwarnasankhaSwarnasankha

If I have understood your requirement correctly, it would mean that every Parent Record (Object 2) can have only one Child Record (Object 1) where Object 1 has a Lookup relationship to Object 2.

 

You did mention that both Object 1 and Object 2 are custom objects - I would like to ask if the relationship is a simple Lookup or a Master-Detail relationship?

 

In case of a simple lookup, you will need custom coding in the form of an Apex Trigger; however, in case of a Master Detail relationship, you can use the concept of a Roll Up Summary field to get a count of child records associated with a parent record and have a validation rule check & fire if the count on the Parent record is greater than 0.

All Answers

youcyouc

if I understand correctly, the lookupfield's value should be unique, then you need to create a trigger to compare the value with all the other records' value of object A.

SwarnasankhaSwarnasankha

If I have understood your requirement correctly, it would mean that every Parent Record (Object 2) can have only one Child Record (Object 1) where Object 1 has a Lookup relationship to Object 2.

 

You did mention that both Object 1 and Object 2 are custom objects - I would like to ask if the relationship is a simple Lookup or a Master-Detail relationship?

 

In case of a simple lookup, you will need custom coding in the form of an Apex Trigger; however, in case of a Master Detail relationship, you can use the concept of a Roll Up Summary field to get a count of child records associated with a parent record and have a validation rule check & fire if the count on the Parent record is greater than 0.

This was selected as the best answer
MBrady325MBrady325

It is a simple lookup record, but I'll look into making it a master-detail to see if that will work.  Thanks!