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
Dharmesh MaheshwariDharmesh Maheshwari 

I want to delete duplicate lead on the basis of email and retain one record with deleted leads name and company in the retain lead description field.

global class removeDuplicateRecords implements Database.Batchable<SObject> 
{ 
    Global Map<String,Lead> EmailLead = new Map<String,Lead>();
    global Map<String,List<String>> mapEmailName = new Map<String,List<String>>();
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Email,Name,Company,Description from Lead where Email != null';
        return Database.getQueryLocator(query); 
    }
    
    global void execute(Database.BatchableContext BC,List<Lead> scope)
    { 
        List<String> LeadInfo = new List<String>();
        List<Lead> duplicatelist = new List<Lead>();
        for(Lead objLead : scope)
        {
            if(!EmailLead.containsKey(objLead.Email))
            {
                EmailLead.put(objLead.Email,objLead);
            }
            else
            {
                duplicatelist.add(objLead);	
            } 
        } 
        System.debug('>>duplicatelist>>>'+duplicatelist);
        System.debug('>>>EmailLead>>'+EmailLead);
        for(Lead objLead1 : duplicatelist)
        {
            LeadInfo.add(objLead1.Name+','+objLead1.Company);
            if(mapEmailName.containsKey(objLead1.Email))
            {
                LeadInfo.clear();
                LeadInfo.add(objLead1.Name+','+objLead1.Company);
                mapEmailName.put(objLead1.Email,LeadInfo);
            }
            else
            {
                mapEmailName.put(objLead1.Email,new List<String>(LeadInfo));
            }
        }
        System.debug('>>mapEmailName>>>'+mapEmailName);
        System.debug('>>EmailLead.values>>>'+EmailLead.values());
        for(Lead updateLead : EmailLead.values())
        {
            System.debug('>>updateLead>>>'+updateLead.Email);
            System.debug('>>EmailLead.values>>>'+EmailLead.values());
            if(mapEmailName.containsKey(updateLead.Email))
            {
                String strLead;
                System.debug('>>mapEmailName.get(updateLead.Email)>>>'+mapEmailName.get(updateLead.Email));
                strLead = String.join(mapEmailName.get(updateLead.Email),';');
                System.debug('>>strLead>>>'+strLead);
                updateLead.Description = strLead;
            }
            updateLead.Description.removeEnd(';');
        }
        if(EmailLead.values().size()>0)
            update EmailLead.values();
        if(duplicatelist.size() > 0)
        {
            delete duplicatelist;
        }
    }
    global void finish(Database.BatchableContext BC)
    {
        
    }
}
Suppose I have 5 Lead
2 with a@gmail.com mail address
3 with b@gmail.com mail address
So I want to delete duplicate of both email addres lead. Result is:
1 with a@gmail.com
2 with b@gmail.com
So the name and company of a@gmail.com is come on that lead description field. and another name and company of b@gmail.com come on thanother lead description field.

Above code is perfectly run but I am not able to separate name and company on the basis of email it mix and match all name and company in all leads
Best Answer chosen by Dharmesh Maheshwari
Ajay K DubediAjay K Dubedi
Hi Dharmesh,

The code below works fine the way you want to:
* Duplicate record with the same email gets deleted
* The remaining lead with unique email contains deleted Lead's Name and company in its description field.
 
Batch Class ----->

global class removeDuplicateRecords implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'select Email,Name,Company,Description from Lead where Email != Null';
        return Database.getQueryLocator(query); 
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    { 
        Map<String,Lead> EmailLead = new Map<String,Lead>();
        List<Lead> LeadInfo = new List<Lead>();
        List<Lead> duplicatelist = new List<Lead>();
        for(Lead objLead : scope)
        {
            if(!EmailLead.containsKey(objLead.Email))
            {
                EmailLead.put(objLead.Email,objLead);
            }
            else
            {
                duplicatelist.add(objLead);
            }
        }
        for(Lead objLead1 : duplicatelist)
        {
            Lead lead_new = EmailLead.get(objLead1.Email);
            if(lead_new.Description != Null)
            {
                lead_new.Description = lead_new.Description + (';'+ objLead1.Name +','+ objLead1.Company);
                LeadInfo.add(lead_new);
            }
            else
            {
                lead_new.Description = (objLead1.Name +','+ objLead1.Company);
                LeadInfo.add(lead_new);
            }
        }
        if(LeadInfo.size() > 0)
        {
            update LeadInfo;
        }
        if(duplicatelist.size() > 0)
        {
            delete duplicatelist;
        }
    }
    global void finish(Database.BatchableContext BC)
    {
        
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com