Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a custom object. I want to put in a business rule so that a combination of two fields must be unique.

So I have Field1 and Field2 and want the combination to be unique:

Field1=4, Field2=7
Field1=4, Field2=8

is ok

But,

Field1=4, Field2=7
Field1=4, Field2=7

is not ok

What is the easiest / best way to do this? Thanks.

share|improve this question

2 Answers 2

The easiest way to do this is to create a Unique Text field. Then, concatenate the values of the two fields together and update your Unique Text Field with the resulting value using a Workflow Field Update or a before Trigger.

Smashing the two fields together would essentially create a composite key that can be used to verify uniqueness.

share|improve this answer
2  
Innovative use of declarative platform features here :) –  user320 Feb 26 '13 at 16:07
    
Thank you for the compliment! –  Kevin O'Hara Feb 26 '13 at 16:17
1  
If you go for workflow remember to design criteria that include ISNEW(), ISCHANGED(), ISBLANK(unique_field__c) etc :) Also careful with any datafixes that'd fill the field from Apex - Id fields (lookups) have 18 chars when queried in apex but workflows see only 15 and that's what will be filled in to the unique field. –  eyescream Feb 26 '13 at 17:10

I would create a BEFORE trigger. You'll need to run a SOQL query with those values and reject the save if any matching records are found.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.