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
Abilash.SAbilash.S 

trigger to prevent duplicate email id

Write a trigger to prevent duplicate email id in contact.
Please dont share any related links on Duplicate preventions. Help me on writing code what exactly need to apply.
Best Answer chosen by Abilash.S
CharuDuttCharuDutt
Hii Pavushetti Abhilash
Try The Below Code
trigger PreventDuplicateContacts on Contact (before insert) {
    Set <String> emailSet = new Set<String>(); 
    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() > 0) {
          con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
        }
    }
}

Please Mark it As Best Anser if it Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Pavushetti Abhilash
Try The Below Code
trigger PreventDuplicateContacts on Contact (before insert) {
    Set <String> emailSet = new Set<String>(); 
    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() > 0) {
          con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
        }
    }
}

Please Mark it As Best Anser if it Helps
Thank You!
This was selected as the best answer
Abilash.SAbilash.S
Perfect code. I have implemented. Zero errors. 
thankyou Charudutt.