+ Start a Discussion
Salluri LeelavathiSalluri Leelavathi 

I am trying to create a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.

I tried this code but not working. Can any one help me on this?

trigger Contactduplicaterecord on Contact (before insert, before update) 
{
  set<string> EmaPho=new set<string>();
    
    for(Contact Con:trigger.new)
    {
        EmaPho.add(Con.Phone);
        EmaPho.add(Con.Email);
     }
    if(EmaPho.size()>0 )
    {
    List<Contact> lstContact=[Select Phone,Email from Contact where Phone in:EmaPho or Email in:EmaPho];
    Map<string, Contact> MapEmaPhowisecontact=new Map<string, Contact>();
    
    for(Contact Con:lstContact)
    {
        MapEmaPhowisecontact.put(Con.Phone, Con);
        MapEmaPhowisecontact.put(Con.Email, Con);
    }
    for(Contact Con:trigger.new)
    {
        if(MapEmaPhowisecontact.containsKey(Con.Phone))
        {
            Con.Phone.addError('Phone Number already Exist');
        }
        if(MapEmaPhowisecontact.containsKey(Con.Email))
        {
            Con.Email.addError('Email already Exist');
        }
    }
    }
}
Best Answer chosen by Salluri Leelavathi
Maharajan CMaharajan C
Hi Leela,

I have update your same code... there is minor changes and it's working fine my dev org...
 
trigger Contactduplicaterecord on Contact (before insert, before update) {
    
    set<string> EmailSet=new set<string>();
    set<string> PhoneSet=new set<string>();
    
    for(Contact Con:trigger.new)
    {
        if(Con.Phone != null)
        	PhoneSet.add(Con.Phone);
        if(Con.Email != null)
        	EmailSet.add(Con.Email);
    }
    if(PhoneSet.size()>0 || EmailSet.size()>0 )
    {
        List<Contact> lstContact=[Select Phone,Email from Contact where Phone in:PhoneSet or Email in:EmailSet];
        Map<string, Contact> MapEmaPhowisecontact=new Map<string, Contact>();
        set<string> EmailExistSet=new set<string>();
    	set<string> PhoneExistSet=new set<string>();
        
        for(Contact Con:lstContact)
        {
            if(Con.Email != null)
            	EmailExistSet.add(Con.Email);
            if(Con.Phone != null)
            	PhoneExistSet.add(Con.Phone);
        }
        for(Contact Con:trigger.new)
        {
            if(PhoneExistSet.contains(Con.Phone))
            {
                Con.Phone.addError('Phone Number already Exist');
            }
            if(EmailExistSet.contains(Con.Email))
            {
                Con.Email.addError('Email already Exist');
            }
        }
    }
}

Thanks,
Maharajan.C

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Leelavathi,

Please try with below code.
 
trigger PreventDuplicateContact on Contact (before insert) {
    
    // Set to store email ids
    Set <String> emailSet = new Set<String>(); 
    // Set to store phone numbers
    Set <String> phoneSet = new Set<String>(); 
    
    // Iterate through each Contact and add their email and phone number to their respective Sets
    for (contact con:trigger.new) {
        emailSet.add(con.email);
        phoneSet.add(con.phone);
    }

    // New list to store the found email or phone numbers
    List <Contact> contactList = new List<Contact>();

    // Populating the list using SOQL
    contactlist = [SELECT email,phone FROM Contact WHERE email IN :emailSet OR phone IN :phoneSet];

    // Iterating through each Contact record to see if the same email or phone was found
    for (contact con:trigger.new) {
        If (contactList.size() > 0) {
            // Displaying the error message
            con.adderror( 'Duplicate contact found with phone/email.' );
        }
    }

}

If this helps, please mark it as best answer.

Regards,
Ankaiah
CharuDuttCharuDutt
Hii Salluri
Try Below trigger
trigger PreventDuplicateContacts on Contact (before insert) {
    Set <String> emailSet = new Set<String>(); 
    Set <String> PhoneSet = new Set<String>(); 
    for (contact con:trigger.new) {
     if(Con.Email != null){        
        emailSet.add(con.Email);
      }
     if(Con.Phone != null){        
        PhoneSet.add(Con.Phone);
      }
        
    }
    List <Contact> contactList = [SELECT Email,Phone FROM Contact WHERE email IN :emailSet AND Phone In :PhoneSet ];

    for (contact con:trigger.new) {
        If (contactList.size() > 0) {
            
          con.Email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
            
        }
    }
Please Mark It As Best Answer If It Helps
Thank You!
 
Suraj Tripathi 47Suraj Tripathi 47

Hii Salluri.

Please try with below code.

 

Trigger:-

trigger DuplicateRecord on Contact (before insert) {
DuplicateRecord_handler.beforeInsert(trigger.new);
}

 

Handler:-

public class DuplicateRecord_handler {
    public static void beforeInsert(List<Contact> ConList){
        try{
            Set <String> emailSet = new Set<String>(); 
            Set <String> phoneSet = new Set<String>(); 
            for (contact conObject:ConList) {
                if(conObject.Email != null){        
                    emailSet.add(conObject.Email);
                }
                if(conObject.Phone != null){        
                    PhoneSet.add(conObject.Phone);
                }
            }
            ConList = [SELECT email,phone FROM Contact WHERE email IN :emailSet OR phone IN :phoneSet];
            for (contact con:ConList) {
                If (ConList.size() > 0) {
                    con.adderror( 'Duplicate contact found with phone/email.' );
                }
            }
        }catch(Exception e){
            system.debug('Error'+e.getMessage()+'at Line'+e.getLineNumber());
        }
    } 
}

 

If you find the above solution helpful, then please mark it as Best Answer to help others too.

Thanks and Regards,
Suraj.

Maharajan CMaharajan C
Hi Leela,

I have update your same code... there is minor changes and it's working fine my dev org...
 
trigger Contactduplicaterecord on Contact (before insert, before update) {
    
    set<string> EmailSet=new set<string>();
    set<string> PhoneSet=new set<string>();
    
    for(Contact Con:trigger.new)
    {
        if(Con.Phone != null)
        	PhoneSet.add(Con.Phone);
        if(Con.Email != null)
        	EmailSet.add(Con.Email);
    }
    if(PhoneSet.size()>0 || EmailSet.size()>0 )
    {
        List<Contact> lstContact=[Select Phone,Email from Contact where Phone in:PhoneSet or Email in:EmailSet];
        Map<string, Contact> MapEmaPhowisecontact=new Map<string, Contact>();
        set<string> EmailExistSet=new set<string>();
    	set<string> PhoneExistSet=new set<string>();
        
        for(Contact Con:lstContact)
        {
            if(Con.Email != null)
            	EmailExistSet.add(Con.Email);
            if(Con.Phone != null)
            	PhoneExistSet.add(Con.Phone);
        }
        for(Contact Con:trigger.new)
        {
            if(PhoneExistSet.contains(Con.Phone))
            {
                Con.Phone.addError('Phone Number already Exist');
            }
            if(EmailExistSet.contains(Con.Email))
            {
                Con.Email.addError('Email already Exist');
            }
        }
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
mukesh guptamukesh gupta
Hi Salluri,

Please use below code:- 
 
trigger Contactduplicaterecord on Contact (before insert) 
{
    set<string> emailList=new set<string>();
    set<string> phoneList=new set<string>();
	
	  Map<string, Contact> MapPhoneList=new Map<string, Contact>();
	  Map<string, Contact> MapEmailList=new Map<string, Contact>();

	
    for(Contact Con:trigger.new)
    {
        phoneList.add(Con.Phone);
        emailList.add(Con.Email);
     }
   
    List<Contact> lstContact=[Select Phone,Email from Contact where Phone IN: phoneList OR Email IN:emailList];
	
	
	for(Contact con : lstContact){
	if(con.Phone != Null)
		MapPhoneList.put(con.Id, con.Phone);
	 
	 if(con.Email != Null)
		MapEmailList.put(con.Id, con.Email);
	}
	
    for(Contact Con:trigger.new)
    {
		if(MapPhoneList.containsKey(con.Phone))
        {
            con.Phone.addError('Phone Number already Exist');
        }
        if(MapEmailList.containsKey(con.Email))
        {
            con.Email.addError('Email already Exist');
        }
    }
    }
}

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

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

Thanks
Mukesh
Salluri LeelavathiSalluri Leelavathi
Thank you for all who helped me in this. Each code has its own style and learned how to write this in different way. Thanks again mukesh gupta garu, Maharajan C garu, Suraj Tripathi 47 garu,and Ankaiah garu.