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
DipthiDipthi 

Trigger preventing duplicates - Plz help..I am struck

trigger PreventDuplicateOnContact on Contact (before insert) {
    // Set to store Phone No's
    Set<String> ContactPhoneSet = new Set<String> ();
    // Set to store Email ID's
    Set<String> ContactEmailSet = new Set<String> ();
    
    // Iterate through each new Contact and add their email and Phone in to Sets 
    for(Contact Con : Trigger.new) {
        ContactPhoneSet.add(con.Phone);
        ContactPhoneSet.add(con.Email);
    }
   
   List<Contact> ContactPhoneList = new List<Contact>();
   List<Contact> ContactEmailList = new List<Contact>();
    
    
   ContactEmailList = [Select email From Contact Where Email IN : ContactEmailSet];
   ContactPhoneList = [Select Phone From Contact Where Phone IN : ContactPhoneSet];
   
   for(Contact Con : Trigger.new) {
       If(ContactEmailList.size() > 0) {
           Con.Email.AddError('Duplicate Contact Found With Same Email');
           System.debug('Email'); 
         }
        
       If(ContactPhoneList.size() > 0) {
          Con.Phone.AddError('Duplicate Contact Found With Same Phone');
        }
  }  
    
}
Best Answer chosen by Dipthi
DipthiDipthi
I made modifications to the above code. Its all working fine. But while creating CONTACT record, if I leave empty Phone or Email it is showing error. Can I avoid this situation ?

trigger PreventDuplicateOnContact on Contact (before insert) {

    // Set to store Phone No's
    Set<String> ContactPhoneSet = new Set<String> ();
    // Set to store Email ID's
    Set<String> ContactEmailSet = new Set<String> ();
    
    // Iterate through each new Contact and add their email and Phone in to Sets 
    for(Contact con : Trigger.new) {
        ContactPhoneSet.add(con.Phone);
        ContactEmailSet.add(con.Email);
    
    // New List to store email and Phone numbers
    List<Contact> ContactPhoneList = [ SELECT Phone FROM Contact WHERE Phone IN :ContactPhoneSet];
    List<Contact> ContactEmailList = [ SELECT email FROM Contact WHERE email IN :ContactEmailSet];
    
    
        If(ContactPhoneList.size() > 0) {
            con.Phone.adderror ('Duplicate Contact with same Phone');
        }
        
           If(ContactEmailList.size() > 0) {
            con.Email.adderror ('Duplicate Contact with same Email');
         }

  }    
}

All Answers

AbhishekAbhishek (Salesforce Developers) 
https://www.syedtaha.com/salesforce-apex-triggers/salesforce-apex-trigger-to-prevent-duplicate-contacts-by-email-or-phone-number/897/

The above blog has a code snippet, you have to make changes based on your requirement.


Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
DipthiDipthi
The code in above blog works good to display message on top of page.
But can you please help me to display error message at the appropriate field (Phone/Email) 
charu tekadecharu tekade
trigger DUPLICON on Contact (before insert) 
{
    LIST<CONTACT> CD=TRIGGER.NEW;
    FOR(CONTACT C:CD)
    {   
        LIST<CONTACT> mylist=[SELECT ID,EMAIL FROM CONTACT WHERE EMAIL=:C.EMAIL ];
        LIST<CONTACT> mylist1=[SELECT ID,PHONE FROM CONTACT WHERE EMAIL=:C.phone ];
    IF(mylist.size() > 0)
    {
        C.EMAIL.ADDERROR('THIS EMAIL ID EXIST');
        
    }
     else if(mylist1.size() > 0)
    {
        C.phone.ADDERROR('THIS Phone number EXIST');
        
    }
     }
     

}
DipthiDipthi
@Charu Tekade, It is showing error msg on both fields  if I enter duplicate Phone and new email.  I should see error msg only for Phone not Email
 
DipthiDipthi
I made modifications to the above code. Its all working fine. But while creating CONTACT record, if I leave empty Phone or Email it is showing error. Can I avoid this situation ?

trigger PreventDuplicateOnContact on Contact (before insert) {

    // Set to store Phone No's
    Set<String> ContactPhoneSet = new Set<String> ();
    // Set to store Email ID's
    Set<String> ContactEmailSet = new Set<String> ();
    
    // Iterate through each new Contact and add their email and Phone in to Sets 
    for(Contact con : Trigger.new) {
        ContactPhoneSet.add(con.Phone);
        ContactEmailSet.add(con.Email);
    
    // New List to store email and Phone numbers
    List<Contact> ContactPhoneList = [ SELECT Phone FROM Contact WHERE Phone IN :ContactPhoneSet];
    List<Contact> ContactEmailList = [ SELECT email FROM Contact WHERE email IN :ContactEmailSet];
    
    
        If(ContactPhoneList.size() > 0) {
            con.Phone.adderror ('Duplicate Contact with same Phone');
        }
        
           If(ContactEmailList.size() > 0) {
            con.Email.adderror ('Duplicate Contact with same Email');
         }

  }    
}
This was selected as the best answer
DipthiDipthi
I believe the above modified code is working as intended . Some of my already existing accounts have empty values on Phone and Email fields and which is why while creating a new record ,if I am leaving any empty Phone or email I am seeing Error.