We have recently enabled Data.com Duplicate Management System to prevent duplicates from getting created for Leads, Contacts and Accounts. It works fine. But we also want to remove duplicates from the existing system matching our rules. Is there a way to do this?


Please Check the following link for apps that may help you.



Appexchange Tools

You can do this using multiple Appexchange tools like couldingo, Dupechecker witht he scheduling option to run on existing and you can specify the no:of fields you need to verify and merge the records and the option to set the master record among the available dupes.However these tools are costly and on a yearly subscription.

Batch Apex:

You can write a batch apex class for the merging of upto 3 records. Here is the sample code that will give you a start.

// Insert new accounts
List<Account> ls = new List<Account>{
new Account(name='Acme Inc.'),
    new Account(name='Acme')
insert ls;

// Queries to get the inserted accounts 
 Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
 Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];

// Add a contact to the account to be merged
 Contact c = new Contact(FirstName='Joe',LastName='Merged');
 c.AccountId = mergeAcct.Id;
 insert c;

try {
 merge masterAcct mergeAcct;
 } catch (DmlException e) {
 // Process exception
System.debug('An unexpected error has occurred: ' + e.getMessage()); 

// Once the account is merged with the master account,
// the related contact should be moved to the master record.
masterAcct = [SELECT Id, Name, (SELECT FirstName,LastName From Contacts) 
          FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
System.assert(masterAcct.getSObjects('Contacts').size() > 0);
System.assertEquals('Joe', masterAcct.getSObjects('Contacts')[0].get('FirstName'));
System.assertEquals('Merged', masterAcct.getSObjects('Contacts')[0].get('LastName'));

// Verify that the merge record got deleted
Account[] result = [SELECT Id, Name FROM Account WHERE Id=:mergeAcct.Id];
System.assertEquals(0, result.size());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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