• Abbas Raza
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi,

Below is a piece of trigger to prevent the creation of Duplicate on whole org basis,

trigger ContactDuplicatePreventer on Contact(before insert, before update){
   Map<String, Contact> contactMap =new Map<String, Contact>();
    for (Contact contact : System.Trigger.new){
if ((contact.Email !=null) && (System.Trigger.isInsert ||(contact.Email != System.Trigger.oldMap.get(contact.Id).Email))){
  if (contactMap.containsKey(contact.Email)){
contact.Email.addError('Another new contact has the '+'same email address.');
            }else{
                contactMap.put(contact.Email, contact);
     }
       }
    }
    for (Contact contact : [SELECT Email FROM Contact WHERE Email IN :contactMap.KeySet()]){
        Contact newContact = contactMap.get(contact.Email);
        newContact.Email.addError('A Contact with this email '+'address already exists.');
   }
}

But i am looking to restrict duplicates only on Account records i.e. it should prevent duplicate contacts in case the the duplicate is on the same Account, if Account is not the same then it should allow duplicates.

Thank You

  • August 26, 2014
  • Like
  • 0