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
DJ MaheshwariDJ Maheshwari 

I want to merge Lead on the basis of phone number but the condition is lead comparison done from both existing and lead in the trigger list (for bulkified).

Here is my code, but it won't work. Following are the cases and assume phone numbers are same.
Case 1:
Lead1(Existing Lead) -
Field1 - Null
Field2 - Value 2
Field3 - Null

Lead2(In TriggerList) - 
Field1 - Value1
Field2 - Null
Field3 - Value 3

Lead3(In Same TriggerList as Lead2) - 
Field1 - Value 5
Field2 - Value 6
Field3 - Null

When Lead is merge then Final Lead wil become:
Lead1 -
Field1 - Value 5
Field2 - Value 6
Field3 - Value 3

And Lead2 and Lead3 will delete.
public class MergeLead
{
    Integer i=0;
    Map<String, Schema.SObjectField> schemaFieldMap2 = Schema.SObjectType.Lead.fields.getMap();
    List<Lead> lstLeadToDelete = new List<Lead>();
    public static Boolean runOnce = true;
    public void onInsert(List<Lead> lstInsertLeadTrigger)
    {
        i++;
        System.debug('++++iiiiii'+i);
        if(runOnce)
        {
            InsertLead(lstInsertLeadTrigger);
        }
    }
    private void InsertLead(List<Lead> lstLeadInsert)
    {
        set<String> setPhone = new set<String>();
        Map<String,Lead> mapLead = new Map<String,Lead>();
        for(Lead objLd : lstLeadInsert)
        {
            if(!setPhone.contains(objLd.Phone))
            {
                setPhone.add(objLd.Phone);
            }            
        }
        Set<String> fieldNames = schemaFieldMap2.keySet();
        
        // Build a Dynamic Query String.
        String soqlQuery = ' SELECT ' + string.join (new List<String>(fieldNames), ',') + ' FROM Lead Where Phone =: setPhone';
        System.debug('+++++fieldNames+++++'+string.join (new List<String>(fieldNames),','));
        System.debug('++++setPhone++++'+setPhone);
        List<Lead> lstLeadToCheck = new List<Lead>();
        if(setPhone != null)
        {
            lstLeadToCheck = Database.query(soqlQuery);
        }
        System.debug('++++lstLeadToCheck++++'+ lstLeadToCheck);
        if(lstLeadToCheck.size()>0)
        {
            for(Lead objOldLead : lstLeadToCheck)
            {
                mapLead.put(objOldLead.Phone, objOldLead);
            }
        }
        System.debug('++++mapLead++++'+mapLead);
        for(Lead objLead : lstLeadInsert)
        {
            System.debug('++++mapLead.containsKey(objLead.Phone)++++'+mapLead.containsKey(objLead.Phone));
            if(mapLead.containsKey(objLead.Phone))
            {
                System.debug('+++mapLead.get(objLead.phone).id++++'+mapLead.get(objLead.phone).id);
                System.debug('++++objLead.id+++'+objLead.id);
                if(mapLead.get(objLead.phone).id != objLead.id)
                    lstLeadToDelete.add(objLead); 
                for(String strFields : schemaFieldMap2.keySet())
                {
                    if(mapLead.get(objLead.phone).get(strFields) == null && objLead.get(strFields) != null)
                    {
                        mapLead.get(objLead.phone).put(strFields,objLead.get(strFields));
                    }
                }
                System.debug('++++mapLead++++'+mapLead);
            }
        }
        if(mapLead.size()>0)
        {
            System.debug('+++++mapLead++++'+mapLead);
            update mapLead.values();
        }
        System.debug('++++lstLeadToDelete+++'+lstLeadToDelete);
        if(lstLeadToDelete.size()>0)
        {
            delete lstLeadToDelete;    
        }
        System.debug('++++lstLeadToDelete+++'+lstLeadToDelete);
    }  
}