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
Abhishek Kumar 407Abhishek Kumar 407 

Can someone help me with below use case:-

 When Lead is creating / updating - we need to find If any lead with same email exist If yes then merge all the lead with same email adress.

 Lead which has Max. "Last Sync Time" value [for same email adress] will consider as Master Lead and all other duplicate leads will be considered as child leads  and  all those child lead will merge to Master lead.
Below are four main fields which we will append value so no data loss will happened in below fields , rest all we will merge and who so ever is master lead fields will be the Winner and rest lead fields value will be discarded :

Description

here is the code, which I am trying
//Trigger

trigger leadMergeTrigger on Lead (after insert, after update)
{
	leadMergeTriggerHandler handler=new leadMergeTriggerHandler();
	handler.leadMerge(Trigger.new);   
}
 
//trigger handler class

public class leadMergeTriggerHandler
{
  public void leadMerge(List<Lead> leadList)
    {
       //Creating an object of List<String> to store email addresses
        List<string> email=new List<string>();
        
        //foreach loop to take email from trigger.new
        for(lead led : leadList)
        {
            //add the Email addresses to the List<String>
            email.add(led.Email);
        }
        
        //Query on Lead to fetch id,name and email
        List<lead> leadList1=[select id,Name,email from lead];
         
        //foreach loop to take records from Lead Object 
        for(Lead led1 : leadList1)
        {
            //foreach loop to take email from List<String> email and transfer it one by one to 'email1' of string type
            for(string email1 : email)
	         {
                //compare the email address, if same then merge the lead
                 if(led1.Email==email1)
		        {
                           
//what to do now
                    //[Select Id, Name, Last_Sync_Time__c from Lead order by Last_Sync_Time__c asc LIMIT 1];
                    
                }
                 
             }
            
        }
         
        
    }

}