You need to sign in to do that
Don't have an account?
M Sreekanth
There are no errors in my code,my question was is this best practice i did if it have any defect or another way please tell mes
Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.But I want Indivizual Error detection,I did like below
--------------------------------------------------------------------------------------
Heleper Class:-
==========
public class ContactHelper {
//Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.But I want Indivizual Error detection
public static void PreventDuplicateContacts(list<contact> conList){
list<string> dupEmail =new list<string>();
list<string> dupPhone =new list<string>();
list<contact> EmailList = new list<contact>();
list<contact> PhoneList = new list<contact>();
for(contact c:conList){
dupEmail.add(c.Email);
dupPhone.add(c.Phone);
}
EmailList = [SELECT Id,Email FROM Contact WHERE Email =:dupEmail];
PhoneList = [SELECT Id,Phone FROM Contact WHERE Phone =:dupPhone];
for(contact c:conList){
if(EmailList.size()>0){
c.Email.addError('Email id already exists');
}
if(PhoneList.size()>0){
c.Phone.addError('Email id already exists');
}
}
}
}
Handler Class:-
==========
public class ContactHandler {
public static void PreventDuplicateContacts(){
ContactHelper.PreventDuplicateContacts(Trigger.new);
}
}
Trigger Code:-
=========
trigger ContactTrigger on Contact (Before insert,Before Update) {
if(Trigger.isInsert){
ContactHandler.PreventDuplicateContacts();
}
else if(Trigger.isUpdate){
ContactHandler.PreventDuplicateContacts();
}
}
--------------------------------------------------------------------------------------
Heleper Class:-
==========
public class ContactHelper {
//Write a trigger on contact to prevent duplicate records based on Contact Email & Contact Phone.But I want Indivizual Error detection
public static void PreventDuplicateContacts(list<contact> conList){
list<string> dupEmail =new list<string>();
list<string> dupPhone =new list<string>();
list<contact> EmailList = new list<contact>();
list<contact> PhoneList = new list<contact>();
for(contact c:conList){
dupEmail.add(c.Email);
dupPhone.add(c.Phone);
}
EmailList = [SELECT Id,Email FROM Contact WHERE Email =:dupEmail];
PhoneList = [SELECT Id,Phone FROM Contact WHERE Phone =:dupPhone];
for(contact c:conList){
if(EmailList.size()>0){
c.Email.addError('Email id already exists');
}
if(PhoneList.size()>0){
c.Phone.addError('Email id already exists');
}
}
}
}
Handler Class:-
==========
public class ContactHandler {
public static void PreventDuplicateContacts(){
ContactHelper.PreventDuplicateContacts(Trigger.new);
}
}
Trigger Code:-
=========
trigger ContactTrigger on Contact (Before insert,Before Update) {
if(Trigger.isInsert){
ContactHandler.PreventDuplicateContacts();
}
else if(Trigger.isUpdate){
ContactHandler.PreventDuplicateContacts();
}
}
You need to write Handler and helper. Both are same you can directly write the trigger code as below.
You can remove Handler class entirely.
I did not find any issues other than this.
Let me know if you face any issues.
Thanks,
All Answers
You need to write Handler and helper. Both are same you can directly write the trigger code as below.
You can remove Handler class entirely.
I did not find any issues other than this.
Let me know if you face any issues.
Thanks,
Thanks Sai for your reply 🙂
yes I dont see any issues in the code inside the helper.
You can mark the above asnwer as best answer so it helps others.
Thanks,