+ Start a Discussion
m 3m 3 

how can you delete duplicate records are names in salesforce

how can you delete duplicate records are contacts are accounts in salesforce.
when business process is going in that time duplicates is creating how can you achieve this 
NagendraNagendra (Salesforce Developers) 
Hi,

May I request you please elaborate your issue on how do you want to delete duplicate records?

Is that based on a field or a particular condition etc?

If you are trying to delete duplicate records based on a particular field then I would suggest you please search with the respective field and merge the records rather than deleting the records.

Below is the piece of code which deletes the duplicate records based on a particular field.
global class removeDuplicateRecords implements Database.Batchable<SObject> , Database.Stateful {
    
    global Set<String> emailstring;
    
    global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator([Select Email__c from Book3__c where Email__c != null]);
      }
    
    global void execute(Database.BatchableContext BC , List<Book3__c> scope){
        
        Set<String> emailstring = new Set<String>();
		List<Book3__c> duplicatelist = new List<Book3__c>();
        
        Map<String , Book3__c> EmailBookmap = new Map<String , Book3__c>();
        for(Book3__c s : scope){
			if(!EmailBookmap.containsKey(s.Email__c)){
				EmailBookmap.put(s.Email__c , s);
			}
			else{
				duplicatelist.add(s);			
			}                        
        }  
       // system.debug(EmailBookmap);
        /*system.debug(emailstring);
        
        List<Book3__c> bklst = [select Email__c from Book3__c where Email__c In :emailstring ];
        
		for(Book3__c bk : Scope){
        for(Book3__c b :bklst){
           // if(emailstring.contains(b.Email__c)){
            if(bk.email__c == b.email__c){
              duplicatelist.add(b);  
            }
            else{
             emailstring.add(bk.Email__c);   
            }
        }
        }*/
        
       // system.debug(EmailBookmap);
        system.debug(duplicatelist);
		if(duplicatelist.size() > 0){
			delete duplicatelist;
		}
    }
    
    global void finish(Database.BatchableContext BC){
        
    }
        

}
For more information please refer to below links. Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra