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.

  • How does this question not yet have an accepted answer? Ah OP Hasn't been on since 2013.
    – Adrian Larson
    Commented Feb 21, 2018 at 22:34

5 Answers 5


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.

  • 3
    Innovative use of declarative platform features here :) Commented Feb 26, 2013 at 16:07
  • 3
    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
    Commented Feb 26, 2013 at 17:10
  • 2
    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
    Commented Jan 21, 2015 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 Commented May 19, 2015 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: http://releasenotes.docs.salesforce.com/en-us/spring15/release-notes/rn_sales_duplicate_management_intro.htm#rn_sales_duplicate_management_intro

  • 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". Commented Jun 4, 2015 at 23:28

I was darned near creating a new trigger, when I realized that it is now 2019, and I am using Salesforce API version 46, which has much easier options for ensuring that a combination of multiple fields are unique.

All you have to do is create a new "Matching Rule" on your desired object (Standard or Custom), activate it, and then create a new "Duplicate Rule" and have it use the "Matching Rule" you just created/activated.

Salesforce have given us a nice YouTube link that does a great of explaining how to go about it:: https://www.youtube.com/watch?v=zy0u5fKBft0

Easy peasy!


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.


Check out this if you're interested in the solution spectrum and look for other better solutions. It's Before event of record triggered flow to update the unique text field. https://sfdcsharepoint.medium.com/how-to-prevent-duplicate-child-records-for-the-same-parent-record-8a27371669e1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .