You need to sign in to do that
Don't have an account?
Nickolaus 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.');
}
}
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.');
}
}
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.
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
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.
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.
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
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?