I need to see an apex code of trigger, on say object 'Account' to prevent any new records from saving. Existing records do have duplicates but we cannot clean/ remove the data, as the requirement has come midway.

The unique feature applies to 2 fields say A(picklist) and B(text) of object Account, (concatenated) where A is a picklist, having values 1,2,3,4 where we need only values (TEXT) 1,2,3 of A to be unique alongside (concatenated with) text field B, and not the value 4.

Again, the requirement is to prevent any new records from saving which match any of the existing records. Duplicates within the existing records can be ignored.

Thanks in advance.

  • 2
    Can you edit your question and provide some code to demonstrate what you've attempted in meeting this requirement so far?
    – Mark Pond
    Sep 19, 2013 at 6:35
  • as of now, I was only trying to do this via config, using a unique field, then updating the concatenated fields A and B via a workflow. I tried to update existing records through this code -
    – Subham
    Sep 19, 2013 at 8:31
  • List <Secondary_Customer_Details__c> myList = [ SELECT ID, Dup_Test__c , Identity_Proofs__c , Identity_ProofNo__c FROM Secondary_Customer_Details__c WHERE Dup_Test__c = null or Dup_Test__c ='']; for(Secondary_Customer_Details__c o : myList) { o.Dup_Test__c= o.Identity_Proofs__c + '' + o.Identity_ProofNo__c; } update myList ; -- but I am getting update error , since I already have duplicates in my existing records.
    – Subham
    Sep 19, 2013 at 8:32

2 Answers 2


You can potentially do this via config. Create a text field for a composite primary key and mark this as External Id, Unique.

Create a workflow that concatenates the two values and write it to this field on everytime a record is created or updated.

This should automatically give you an error owing to the uniqueness constraint on the field.

  • Thing is I already have existing records which have duplicates. I have already tried your approach. The field updates only for records saved after the new workflow has been created. What about preventing duplicates from records already present before the creation of the workflow. Also writing an apex code to update the empty new unique field fails, since being uniquely indexed, it throws an error on update.
    – Subham
    Sep 19, 2013 at 8:27
  • If you already have duplicates they will need to be removed/resolved before ANY approach can be taken to create a unique key. After the duplicates are removed you can use the Data Loader or some other technique to trigger the workflow or seed the unique values.
    – Mike Chale
    Sep 19, 2013 at 12:29
  • Above solution works fine but what if i want to restrict duplicate records for any child records.Account is my standard object and i have a custom object called Contract__c. I want to restrict the duplicate creation of Contract records under one particular account record.Any suggestion would be very helpful.Thank u. Nov 17, 2014 at 12:37

I would also suggest the ExternalId solution, but I guess deduplication of existing records is not an option...

So you can only do this with a trigger.

1) Create a formula field on your Object which will store the value of your unique key. lets call it myKey__c;

2) In a trigger do the following

//Loop to retrieve all myKey__c values
Set <String> smyKeys = new Set<String>();
for (Account nAcc:Trigger.new)

//SoQL to get existing Accounts with this key

List <Account> lAccounts = [SELECT Id, myKey__c FROM Account where myKey__c in :smyKeys];

//Create a Map <myKey__c -> Account> You can use just a set of MyKey__c if you only want to detect duplicates. With the Map you can also add a more descriptive message
Map <String,Account> mKeyToAccountId = new Map<String,Account>(); 

for (Account a:lAccounts)

//Loop again through your Updated/Inserted accounts
for (Account aNew:Trigger.New)
    if (mKeyToAccountId.containsKey(aNew.myKey__c))
          //Here you may require some more processing if you want to exclude 1 value from being unique 
        aNew.addError('whatever message...'); 

Hope this helps (apologies for any compilation errors didn't check it ) but still I recommend the External id approach

You must log in to answer this question.

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