function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam111Sam111 

Batch Class - Delete Duplicate records

We have a custom object and around 2 million records. We would like to do a clean up on duplicate records. Like if they have same name, same email, opt out flag etc. I would like to create a batch class to delete the duplicate records and just keep one record.

For ex:

Record1: Name = Name1 Email = name1@name.com opt out = true created date 01/01
Record 2:  Name = Name1 Email = name1@name.com opt out = true created date 02/02
Record 3: Name = Name1 Email = name1@name.com opt out = true created date 03/03

I woul dlike to delete 2 records and keep the most recent created date record. So in this case, delete Record 1 an dRecord 2 and keep Record 3

Can someone help with this batch class? I appreciate your help!
karthikeyan perumalkarthikeyan perumal
Hello Sam, 

Try below App to clean your duplicate reocrds and keep the recent or you can merge it. 
https://appexchange.salesforce.com/listingDetail?listingId=a0N300000058vzKEAQ (http:// https://appexchange.salesforce.com/listingDetail?listingId=a0N300000058vzKEAQ)

hope it will help you. or if you really wanna use Batch apex let me know. 

Thanks
karthik
 
Sean McMickleSean McMickle
Hey Sam111,

We've had to deal with something similar. What you can do is create a map of whatever your duplicate measure is (ours is email), to a list of all records that contain that email.  Select your emails through an aggregate query where the count of emails is > 1, build a list of those. After your list is complete, select 200 of those at a time and eliminate them via the mapping discussed earlier.

Let me know if you have any questions!

Thanks!

-Sean
Sam111Sam111
Hi Karthikeyan - thanks for your reply. do you know any other apps where we can schedule the job to delete the records? We just want to do a clean up on a weekly basis.
Sam111Sam111
Hi Sean - Thanks for your reply. Do you mind if I ask you to send me the code that you used to do the clean up?