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
SFDCDoctorStrangeSFDCDoctorStrange 

Apex Trigger for Merge lead with same email or phone and make new lead master lead

Created this Apex class
public class DuplicateLeadMergeHandler 
{    
    public static void MergeLead(List<Lead> newLead)
    {
        Set<String> emails = new Set<String>();
        Set<String> phones = new Set<String>();
        Map<String, Lead> masters = new Map<String, Lead>();
        
        for(Lead ld : newLead)
        {
            emails.add(ld.Email);
            phones.add(ld.Phone);
        }
        
        emails.remove(null);
        phones.remove(null);
        
        for(Lead ld: [SELECT Id,Email,Phone FROM Lead WHERE (Email = :emails OR Phone = :phones) AND Id NOT IN :newLead AND IsConverted = false])
        {
            If(ld.Email != Null)
                masters.put(ld.Email, ld);
            
            else 
                If(ld.Phone != Null)
                masters.put(ld.Phone, ld);
        }
        for(Lead ld : newLead)
        {
            Lead master = new Lead();
            If(masters != Null && masters.size() > 0 && masters.containskey(ld.Email))
            {
                master = masters.get(ld.Email);
            }
            Else   
                If(masters != Null && masters.size() > 0 && masters.containskey(ld.Phone))
            {
                master = masters.get(ld.Phone);
            }

            If(master != null)    
            {     
                merge master ld;
            }
        }
    }  
}
but its showing an errorLead errorplease help me solve this problem

Thanks & Regards 
SFDC Strange
 
mukesh guptamukesh gupta
Hi,

First you need to check validation rules, so open lead object and goto validation rules and check for this error message.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

I have tried the same code. its working for me.

Thanks!!
SFDCDoctorStrangeSFDCDoctorStrange
Hi Mukesh,

We currently don't have any validation rules present in the lead Object right now.

Please check my code and give some suggestions as to it's my first time using the Merge function,

Please help and advise.

Thanks
SFDC Strange
 
SFDCDoctorStrangeSFDCDoctorStrange
Hi Ankaiah,

Is the code merging leads with the same email or phone and making a new lead master lead?

please check and reply

Thanks 
SFDC Strange
mukesh guptamukesh gupta
Hi,

Please use below code:-
 
public class DuplicateLeadMergeHandler 
{    
    public static void MergeLead(List<Lead> newLead)
    {
        Set<String> emails = new Set<String>();
        Set<String> phones = new Set<String>();
        Map<String, Lead> masters = new Map<String, Lead>();
        
        for(Lead ld : newLead)
        {
            emails.add(ld.Email);
            phones.add(ld.Phone);
        }
        
        emails.remove(null);
        phones.remove(null);
        
        for(Lead ld: [SELECT Id,Email,Phone FROM Lead WHERE (Email IN: emails OR Phone IN: phones) AND Id NOT IN: newLead AND IsConverted = false])
        {
            If(ld.Email != Null)
                masters.put(ld.Email, ld);
            
            else 
                If(ld.Phone != Null)
                masters.put(ld.Phone, ld);
        }
        for(Lead ld : newLead)
        {
            Lead master = new Lead();
            If(masters != Null && masters.size() > 0 && masters.containskey(ld.Email))
            {
                master = masters.get(ld.Email);
            }
            Else   
                If(masters != Null && masters.size() > 0 && masters.containskey(ld.Phone))
            {
                master = masters.get(ld.Phone);
            }

            If(master != null)    
            {     
                merge master ld;
            }
        }
    }  
}

for more rfference use this link https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_merge.htm

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
SFDCDoctorStrangeSFDCDoctorStrange
Hi Mukesh,

This code is also not working still showing the same error and I'm not able to understand this error.

Please help me.

Thanks 
Himanshu