User Story
As a sales manager, I want to ensure that we don't create duplicate contacts in our system, so that we can maintain accurate and consistent data. When a sales representative attempts to create a new contact, the system should check if there is already an contact with the same name, email, and phone. If a duplicate contact is found, the system should prevent the new contact from being created and display an error message to the sales representative, explaining that an contact with the same name, email, and phone already exists. By using this tool, we can avoid creating duplicate contacts in our system and maintain a high level of data quality, which is critical for our business success.
Expected Outcome
An contact with the same name, email, and phone already exists.
Code
Apex Trigger
trigger ContactTrigger on Contact (before insert, before update) {
if(Trigger.isInsert && Trigger.IsBefore || Trigger.isUpdate && Trigger.IsBefore){
Set<String> contactKeySet = new Set<String>();
List<Contact> conList = [SELECT Id, FirstName, LastName, Email, Phone FROM Contact WHERE Name != null AND Email !=Null AND Phone != Null];
for(Contact con : conList){
String conKey = con.FirstName + con.LastName + con.Phone + con.Email;
System.debug(conKey);
contactKeySet.add(conKey);
}
for (Contact con : Trigger.new) {
String conKey = con.FirstName + con.LastName + con.Phone + con.Email;
System.debug(conKey);
if (contactKeySet.contains(conKey)) {
con.addError('An contact with the same name, email, and phone already exists.');
}
}
}
}
Video
Video does not exists.