• Nickolaus Johnson
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi Everyone,

I have this trigger where I'd like it to fire before insert, before update. We have 3 emails fields from the NPSP that we'd like the trigger to fire against so that if a new contact is being created, its email address is ran against any possible email within our system so it does not create that record if it finds a match. The 3 fields we have are npe01__HomeEmail__c, npe01__WorkEmail__c, and npe01__AlternateEmail__c. The trigger is working so that if a value is populated within the email field (which happens automatically based on the preferred email selected), and it finds a match, it will throw the error. However, the new email address entered seems to not be running against the email addresses that aren't selected as "preferred". Any ideas how to fix this?

Here is the code:

trigger DupEmail on Contact(before insert, before update) {
  for (Contact myContact: Trigger.new) {

List<Contact> dupes = [SELECT Id FROM Contact WHERE

     (Id != :myContact.Id AND
     Email != null AND
     (Email = :myContact.npe01__WorkEmail__c OR
     Email = :myContact.npe01__AlternateEmail__c OR
     Email = :myContact.npe01__HomeEmail__c))

     ]  
     ;
if (dupes.size() > 0) { String errorMessage = 'Duplicate contact found! '; errorMessage += 'Duplicate record URL = ' + 'https://cs16.salesforce.com' + '/' + dupes[0].Id;
myContact.addError(errorMessage);
} }
  }


Thank you!
Hi Guys,

So I'm terrible at coding and have an issue within our database where we use the NPSP for SF and have the 3 email fields. I want to be able to make a trigger so that if you're creating a new contact within the system, it will compare the email address you're entering to all 3 of the email fields: Personal, Work, Alternate to make sure there is no corresponding email within the system. If it does find an email that matches, then it prevents it from being saved.

I did come across this trigger here, within the dev forums but am not sure how to fine tune it to have it check the other two email fields. Any help is appreciated, thank you!

Trigger:

trigger contactDuplicatePreventer on Contact
                                 (before insert, before update) {
  
      Map<String, Contact> ContactMap = new Map<String, Contact>();
      for (Contact Contact : System.Trigger.new) {
          
          // Make sure we don't treat an email address that
          // isn't changing during an update as a duplicate.
      
          if ((Contact.Email != null) &&
                  (System.Trigger.isInsert ||
                  (Contact.Email !=
                      System.Trigger.oldMap.get(Contact.Id).Email))) {
          
              // Make sure another new Contact isn't also a duplicate
      
              if (ContactMap.containsKey(Contact.Email)) {
                  Contact.Email.addError('Another new Contact has the '
                                      + 'same email address.');
              } else {
                  ContactMap.put(Contact.Email, Contact);
              }
         }
      }
      
      // Using a single database query, find all the Contacts in
      
      // the database that have the same email address as any
      
      // of the Contacts being inserted or updated.
      
      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.');
      }
  }
Hi Guys,

So I'm terrible at coding and have an issue within our database where we use the NPSP for SF and have the 3 email fields. I want to be able to make a trigger so that if you're creating a new contact within the system, it will compare the email address you're entering to all 3 of the email fields: Personal, Work, Alternate to make sure there is no corresponding email within the system. If it does find an email that matches, then it prevents it from being saved.

I did come across this trigger here, within the dev forums but am not sure how to fine tune it to have it check the other two email fields. Any help is appreciated, thank you!

Trigger:

trigger contactDuplicatePreventer on Contact
                                 (before insert, before update) {
  
      Map<String, Contact> ContactMap = new Map<String, Contact>();
      for (Contact Contact : System.Trigger.new) {
          
          // Make sure we don't treat an email address that
          // isn't changing during an update as a duplicate.
      
          if ((Contact.Email != null) &&
                  (System.Trigger.isInsert ||
                  (Contact.Email !=
                      System.Trigger.oldMap.get(Contact.Id).Email))) {
          
              // Make sure another new Contact isn't also a duplicate
      
              if (ContactMap.containsKey(Contact.Email)) {
                  Contact.Email.addError('Another new Contact has the '
                                      + 'same email address.');
              } else {
                  ContactMap.put(Contact.Email, Contact);
              }
         }
      }
      
      // Using a single database query, find all the Contacts in
      
      // the database that have the same email address as any
      
      // of the Contacts being inserted or updated.
      
      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.');
      }
  }