Trigger Scenario: Prevent Duplicate Records Based on Multiple Fields in Salesforce

Trigger to Prevent Duplicate Contacts Using Email or Phone Number

We checked in earlier post how to prevent creating duplicate Records if one field is same. Now in case of checking multiple fields below is the solution:

trigger PreventDuplicateContacts on Contact (before insert) {

// Set to store email ids
Set <String> emailSet = new Set<String>();
// Set to store phone numbers
Set <String> phoneSet = new Set<String>();

// Iterate through each Contact and add their email and phone number to their respective Sets
for (contact con:trigger.new) {
emailSet.add(con.email);
phoneSet.add(con.phone);
}

// New list to store the found email or phone numbers
List <Contact> contactList = new List<Contact>();

// Populating the list using SOQL
contactlist = [SELECT email,phone FROM Contact WHERE email IN :emailSet OR phone IN :phoneSet];

// Iterating through each Contact record to see if the same email or phone was found
for (contact con:trigger.new) {
If (contactList.size() > 0) {
// Displaying the error
con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
}
}
}

 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes