Sign up ×
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


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

3 Answers 3

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
Innovative use of declarative platform features here :) – bigassforce Feb 26 '13 at 16:07
Thank you for the compliment! – Kevin O'Hara Feb 26 '13 at 16:17
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
We have done this a few times, and at least on one occasion had issues when concatenating two lookup IDs with a workflow rule. We ended up using a trigger to concatenate the fields. – Dan Watt Jan 21 at 19:43
Another vote for being careful with workflows and ID fields. The custom field either needs to be case sensitive or use the CASESAFEID() function in the formula. See The mystery of the nondeterministic Salesforce test case – Daniel Ballinger May 19 at 1:12

The Spring 15 release contains configurable dedupe for standard and custom objects.

You can specify in a more advanced and configurable way exactly what conditions make it a duplicate, which may give you what you're looking for AND be more sustainable as complexity increases.

Duplicate Management section of the release notes:

share|improve this answer
It seems like a good idea, but couldn't get it to work for my scenario. I ran into a limitation on the matching rule: "A matching rule can have only one lookup relationship field, but you selected more than one". – Daniel Ballinger Jun 4 at 23:28

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


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.