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
Nickolaus JohnsonNickolaus Johnson 

Trigger Question: Need some Guidance

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.');
      }
  }
Shashikant SharmaShashikant Sharma
Hi,

before answering to your question I would like to know what exactly you are doing. 

1. Do you want that no other contact should exisits in database having similar email values in all 3 fields, means a And search for these 3 email fields.
2. Do you want that no other contact should exisits in database having similar email values in any  of fields, means a Or search for these 3 email fields.

I will then suggest you solution based on your answer.

Nick Johnson 7Nick Johnson 7
Hi Shashikant,

Thank you for the reply. Basically the 2nd question is correct. We want it so that upon inserting (whether it be via single entries or in bulk) if the record being created has an email address that matches another contact's email address in any of the 3 email fields, it returns an error stating this is a duplicate address or it errors out upon import. Hope that makes more sense.

Thank you,

Nick
Shashikant SharmaShashikant Sharma
I would suggest you a trick, create 1 Text (255 )field Text_Email__c make it unique when you create this field.  Do not show this field on layouts.

Create a workflow or trigger to copy the value in this field from the Contact.Email on insert and update. As text email is unique it will fire validation.

You could do similar for other fields. By creating text fields for them. This solution take care of existing duplicate, bulk import all use cases.


Nick Johnson 7Nick Johnson 7
I have came across that workflow before and it does work when attempting to look at one email field. I am not sure how to get it so the workflow cross references all 3 of our email fields without creating 3 separate unique fields, then 3 separate field updates. The only issue with that is then how do I make the workflow look at all 3 unique fields to ensure that the email being entered does not match any of them?
Shashikant SharmaShashikant Sharma

You want any email value entered in any of the field to be unique not in the particaular field but in other two fields as well. 

please confirm I could suggest you trigger code if I understood it right.
 
Nick Johnson 7Nick Johnson 7
Yep! That is correct, it doesn't matter which email field it is in (Work, Personal, or Alternate), we just want the email address to be unique.
Shashikant SharmaShashikant Sharma

Follow this trigger algorithem to solve this, if you can not develop code for it let me know.

Create set of emailAddress

Set<String> setEmailAddress
loop over new list {
if( emailField1 != null ) {

// if contains false means add it if true means duplicate in new list
if (!setEmailAddress.contains(emailField1)) {
add all emailField1 to set
}

}

if( emailField2 != null ) {

if (!setEmailAddress.contains(emailField2)) {
add all emailField2 to set
}

}

if( emailField3 != null ) {

if (!setEmailAddress.contains(emailField1)) {
add all emailField3 to set
}

}
}

Query all the records which matches any of the three email fields in where cluase will be like

Where  ( Email IN : setEmailAddress OR Personal IN : setEmailAddress OR Alternate IN : setEmailAddress )

if you find any record with this that means duplicate in exisiting records
Nick Johnson 7Nick Johnson 7
Thank you! I will use this format and see if I can get this created and update this thread tomorrow. I appreciate all your help thus far!
Nickolaus JohnsonNickolaus Johnson
Hi Shashikant,

So I played with the trigger the majority of the weekend but unfortunately, I was unable to get it to save as I keep encountering this error:

Error: Compile Error: unexpected token: Create at line 1 column 0

This is what the trigger looks like as of right now:

Create set of emailAddress

Set<String> setEmailAddress
loop over new list {
if( npe01__WorkEmail__c != null ) {

// if contains false means add it if true means duplicate in new list
if (!setEmailAddress.contains(npe01__WorkEmail__c)) {
add all npe01__WorkEmail__c to set
}

}

if( npe01__HomeEmail__c != null ) {

if (!setEmailAddress.contains(npe01__HomeEmail__c)) {
add all npe01__HomeEmail__c to set
}

}

if( npe01__AlternateEmail__c != null ) {

if (!setEmailAddress.contains(npe01__WorkEmail__c)) {
add all npe01__AlternateEmail__c to set
}

}
}

Query all the records which matches any of the three email fields in where cluase will be like

Where  ( Work IN : setEmailAddress OR Personal IN : setEmailAddress OR Alternate IN : setEmailAddress )

if you find any record with this that means duplicate in exisiting records 


Any thoughts?