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
SV 9SV 9 

Merge 2 duplicate contacts

Could someone please help me with the after update trigger on contact.
Search contacts when email is updated, if there are more than 2 records, exit the loop, displaying an error message. If there are only 2 potential duplicates which have the same email id, then need to merge those 2 records.
ShivankurShivankur (Salesforce Developers) 
Hi SV 9,

You can use a logic similar to mentioned below in your trigger code for after update context:
Integer loopCount = 0;
for (contact newCont : Trigger.new) {
    for (Contact existingCont : contactList) {
        if (newCont.Email == existingCont.Email) {
            loopCount++;
            if(loopCount==2){
            //Merge logic for Contacts
        } else if (loopCount>2) {
             newCont.Email.addError( 'Duplicate Contact Found. Use Existing Contact with Id ' + existingCont.Id);
        }
    }
}
}

Please note above code may not work as per exact requirement and you can modify the same as per your requirement.

Refer for more examples:
https://salesforce.stackexchange.com/questions/293825/create-a-trigger-to-merge-leads-if-the-email-already-exists-on-a-lead
https://developer.salesforce.com/forums/?id=906F00000008xmaIAA

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
CharuDuttCharuDutt
Hi SV9 
Try Below Trigger
trigger PreventDuplicateContacts on Contact (before insert ,before update) {
    Set <String> emailSet = new Set<String>(); 
    if (Trigger.IsBefore && ( Trigger.IsInsert || Trigger.IsUpdate )) {
    for (contact con:trigger.new) {
        emailSet.add(con.email);
        
    }
}
    List <Contact> contactList = [SELECT email,phone FROM Contact WHERE email IN :emailSet];

    for (contact con:trigger.new) {
        If (contactList.size() > 2) {
          con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
        }else If(contactList.size() == 2){
          /* Record Merge Code*/
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hi SV 9,
Kindly find your solution.
public class TestApex {
    public static void mergeTwoContact(Map<Id,Contact> newMap, Map<Id,Contact> oldMap){
        Set<String> emailSet = new Set<String>();
        for(Id key :newMap.keySet()){
            emailSet.add(newMap.get(Key).Email);
        }
        
        List<Contact> contactList = [Select id, email, Name, AccountId from Contact where email in :emailSet];
        Map<String, List<Contact>> duplicateContact = new Map<String, List<Contact>>();
        
        for(Id key :newMap.keySet()){
            for(Contact con :contactList){
                if(con.email.equals(newMap.get(key).email)) {
                    if(duplicateContact.containsKey(newMap.get(key).email)){
                        List<Contact> conList = new List<Contact>();
                        conList =   duplicateContact.get(newMap.get(key).email);  
                        conList.add(con);
                        duplicateContact.put(con.Email, conList);
                    }else{
                        List<Contact> conList = new List<Contact>(); 
                        conList.add(con);
                        duplicateContact.put(con.Email, conList);
                    }
                }  
            }  
        }
        for(String email : emailSet ){
            List<Contact> conList = duplicateContact.get(email);
            if(conList.size() == 2){
                for(Contact con: conList){
                    con.LastName += con.LastName;
                    // Do your custome logic here according to your business requirement.                    
                }
                
            }
        }
    }
}
If You find any issue, Kindly reach out to me and
If you find your Solution then mark it as the best answer. 

Thanks and Regards
Suraj Tripathi.
SV 9SV 9
Thank you all for the reply.

Should be able to get both the records to merge, should this be in the forloop?

Contact o = [SELECT ID, Email FROM Contact WHERE Email =: emailset];
Contact cons = [SELECT Id,Email FROM Contact WHERE Email In : emailset;
merge o cons;
CharuDuttCharuDutt
Hii SV 9
 Yes i Thnk It Has To Be In For Loop

Please Mark It As Best Answer If It Helps
Thank You!