This trigger ensures that Contact records with duplicate Email, Last Name, and Phone values are not created. It validates existing records and throws an error if duplicates are found.
For hands-on practice, check this YouTube playlist.
Solution:
public class ContactTriggerHandler {
public static void handleBeforeInsert (List<Contact> newRecords) {
List<Contact> existingRecords = [SELECT Id, LastName, Phone, Email FROM
Contact LIMIT 50000];
for (Contact con: newRecords) {
for (Contact existingCon : existingRecords) {
if (con.LastName == existingCon.LastName && con.Email == existingCon.
Email && con.Phone == existingCon.Phone){
con.addError(‘Duplicate Found’);
}
}
}
}
}