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
Babar Hussain 13Babar Hussain 13 

remove Duplicate Records

Hi Team, 
I have one requirement says, I have to delete the duplicate account related contact records. 
Note: Keep the latest one and delete the old one.
So, I wrote the code but it is always deleting the new records and kept the older ones.
I have to delete the older ones and keep the new ones 
Please correct my code ifany mistakes I did

MY Code :
*********************************************************************
global class deleteDuplicateRecords implements Database.Batchable<sObject>, Schedulable {
    
    global Database.QueryLocator start (Database.BatchableContext bc) { 
        String Query = 'Select Id, Name From Account';
        return Database.getQueryLocator(Query);
    }
    global void execute (Database.BatchableContext bc, List<Account> accList){  
        Set<Id> accId = new Set<Id>();
        for(Account a :accList) {
            accId.add(a.Id);
            }  
        List<Contact> con = [Select Id, Name,AccountId, Lastname  from Contact Where AccountId IN: accId];
        List<Contact> dupList = new List<Contact>();
        Set<String> orderObj = new Set<String>();
        for(Contact a: con) {
            if(orderObj.contains(a.Name)) {
dupList.add(a);
            }
            else {
                orderObj.add(a.Name);
            }
        }
        delete dupList;
        }
    global void finish (Database.BatchableContext bc) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
   String[] toAddresses = new String[] {'babar.sanjamala@sbdinc.com'}; 
   mail.setToAddresses(toAddresses); 
   mail.setSubject('Apex Batch Job is done');
   mail.setPlainTextBody('The batch Apex job processed successfully'); 
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        
    }
    
    global void execute(Schedulablecontext sc) {
        deleteDuplicateRecords Acc = new deleteDuplicateRecords();
        Database.executeBatch(Acc,200);
    }

}
 
**********************************************************************
Thanks


 
HarshHarsh (Salesforce Developers) 
Hi Babar,

Please check the below links of stack exchanges having the same.
https://salesforce.stackexchange.com/questions/17068/delete-duplicate-records-but-retain-one-record  

The suggestion is given in the answer about a unique key that you can follow to differentiate between old and new records.

Hope your query is answered above.
Please mark it as Best Answer if the above information was helpful.

Thanks.


Heads up about the Forums shut down

User-added image
Important Update

We appreciate your participation in these Salesforce Discussion Forums! It’s active members like you that keep our amazing community going strong.

At this time, we want to give you a heads up that on December 4, 2023, the discussion forums will shut down and all relevant discussions will migrate to the Trailblazer Community digital platform. This move brings all conversations around Salesforce development together in one place and provides more opportunities for our broader community to connect and share. We will be removing outdated, obsolete, or spam content and migrating only relevant discussions to the Trailblazer Community digital platform.

Starting November 20, you can view discussions but not post new questions or responses. On December 2, you will no longer be able to access the discussion forums from the Salesforce Developers website.

Please take these steps before November 30, 2023, 11:59 p.m. PT to ensure your contributions follow you to the Trailblazer Community:
  1. If you’re not already a member of the Trailblazer Communitysign up for a Trailblazer account using the same login email address associated with your Developer Discussion Forums account. This is crucial.
  2. If you already have a Trailblazer account, and it’s using a different email address from the one you used for your Developer Discussion Forums account, we recommend that you log in to the Trailblazer Community and connect your forums email address to your Trailblazer account.
Once you’re in the Trailblazer Community, join the Migration Support Hub users group to help you navigate this transition.

We will keep you up to date throughout the transition, and we look forward to seeing you joining the developer discussions in the Trailblazer Community!

Sincerely,
The Forums Support Team