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
PankajJhaPankajJha 

how to merge duplicate lead with batch apex based on email

Hi everyone,
I'm new in salesforce. if you know plz reply 
how to merge duplicate lead with batch apex based on email
Actually i'm unable to merge chatter feed, and fields like name, title, company, fax etc.
plz reply in code becoz if you provide me link and i try, again the same problem occurs. not able to merge fields. i posting second time this question because someone replyed with link and that is not working, and when i asked again i got no reply. 
thanks in advance.


global class LeadBatchAviodDuplicacy implements Database.Batchable<sObject> {

public String queryString;

global Database.QueryLocator start(Database.BatchableContext BC) {
     
  queryString = 'SELECT Id, Name, Email, Phone, Fax, Company FROM Lead ';
    return Database.getQueryLocator(queryString);
}
  global void execute(Database.BatchableContext BC, List<Lead> obj) {

List<Lead> leads = [Select Id, Email, Phone, Fax, Company, Name, (Select Id, WhoId from Tasks), (Select Id, WhoId from Events) from Lead ORDER BY CreatedDate];

Map<String,List<Lead>> leadsMap = new Map<String,List<Lead>>();

for( Lead l : leads ) {
    if( !leadsMap.containsKey( l.Email ) ) {
        leadsMap.put( l.Email, new List<Lead>() );
    }

    leadsMap.get( l.Email ).add( l );
}

List<Event> eventsToUpdate = new List<Event>();
List<Task> tasksToUpdate = new List<Task>();
List<Lead> leadsToDelete = new List<Lead>();


for( String email : leadsMap.keySet() ) {
    List<Lead> leadsToMerge = leadsMap.get( email );
    if( leadsToMerge.size() > 1 ) {
        Lead masterLead = leadsToMerge[0];

        for( Integer i = 1; i < leadsToMerge.size(); i++ ) {
            Lead mergedLead = leadsToMerge[i];
            for( Task t : mergedLead.Tasks ) {
                t.WhoId = masterLead.Id;
                tasksToUpdate.add( t );
            }

            for( Event e : mergedLead.Events ) {
                e.WhoId = masterLead.Id;
                eventsToUpdate.add( e );
            }
            leadsToDelete.add( mergedLead );
        }

    }
}

if( eventsToUpdate.size() > 0 ) update eventsToUpdate;
if( tasksToUpdate.size() > 0 ) update tasksToUpdate;
if( leadsToDelete.size() > 0 ) delete leadsToDelete;
}

global void finish(Database.BatchableContext BC) {

}
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Pankaj,

I was unable to find such implementation where you can use batch apex to merge leads where emails are same, instead I was able to find a developer feed thread where there is a similar use case and this was done in a different way can you please have a look at it and in case if this comes in handy can you please choose this as best answer so that it can be used by others in the future.

>> https://success.salesforce.com/answers?id=9063A000000t1RBQAY

Regards,
Anutej