Write a batch to merge and remove the duplication records.

For ex: 2 existing Accounts(Organizations) having same EIN and same Email, then merge and delete one record .

  • If Acc1 is having Name=Acc1,EIN = 01-000040 and email [email protected], Phone = null
  • If Acc2 is having Name=Acc2,EIN = 01-000040 and email [email protected], Phone = 0471-2465046
  • then copy the phone number of Acc2 to Acc1 and copy the Name = Acc2 to Acc1 and then delete the Acc1 record

Code I have tried:

Global class deDuplicateRecords implements Database.Batchable<sObject>,Database.Stateful{
    global Set<String> ein;
    global Set<String> n;
    global Set<String>u;
    global Set<String>p;

    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([Select Name, EIN_c, Website, Phone from Account WHERE EIN_c != Null]);

    global void execute(Database.BatchableContext bc, List<Account>acc){
        List<Account> accToBeDeleted = new List<Account>();
        Map<String, Account> accountMap = new Map<String,Account>();
        for(Account a:acc){        
                accoutMap.put(a.EIN__c, a);
                Account existingAcc = accoutMap.get(a.EIN__c);
                existingAcc.Website = a.Website;
                existingAcc.Phone = a.Phone;
                existingAcc.Name = a.Name;
                accoutMap.put(a.EIN__c, existingAcc);

        update accoutMap.values();
        delete accToBeDeleted;

    global void finish(Database.BatchableContext bc){

Records before updation: enter image description here

After updation: enter image description here

After running the batch using the anonymous code, the duplicate records are not getting deleted. As according to my requirement, if two records have same EIN and same website then and different EIN then the one with the phone number should exist and other should get deleted.

But After runninng the batch, the names of the records with same EIN are becoming the same but the duplicate isn't deleted. Also the phone numbers are becoming blank.

How do I fix this?

  • 1
    You'll need to re-word your requirements. It's a bit confusing not sure what you're trying to do here. Commented Aug 3, 2022 at 16:40
  • 1
    @HaroutTatarian I am trying to write a batch class to merge the accounts which have the same website and EIN in to a single record. Also if any of the records have a phone number then that record's name should be kept while merging these.
    – user115173
    Commented Aug 3, 2022 at 17:09
  • To deduplicate existing records, it is much easier to use a 3rd party tool, check the AppExchange. To prevent creation of new duplicates, it's much easier to use Salesforce duplicate rules and the FindDuplicates class to match new records against existing. Commented Aug 3, 2022 at 22:16

1 Answer 1


You Need to Approach the Problem Differently

You shouldn't loop over records to merge duplicates like this. Even though the logic is wrong I'm not attempting to fix it since it needs to be re-written completely anyways.

The problem with this approach is that you are batching over a random collection of records.

For example, consider this situation:

  • You have 10,000 records in your org
  • You have 2 duplicate records
  • Your batch size is 200

It's unlikely that the 2 records will be processed in the same batch (cause batch size is 200) so the duplicated record won't be found.

The solution to this depends on the number of records in your org and the number of duplicates you are expecting to find.

Things to consider using:

  • Database.Stateful
  • Group By EIN number
  • Having a temporary storage for EINs found

You must log in to answer this question.

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