function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
M SreekanthM 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();
    }
}
Best Answer chosen by M Sreekanth
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sreekanth,

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.
trigger ContactTrigger on Contact (Before insert,Before Update) {
    if(Trigger.isInsert || Trigger.isupdate ){
        ContactHelper.PreventDuplicateContacts(Trigger.new);
    }
}

Let me know if you face any issues.


Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sreekanth,

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.
trigger ContactTrigger on Contact (Before insert,Before Update) {
    if(Trigger.isInsert || Trigger.isupdate ){
        ContactHelper.PreventDuplicateContacts(Trigger.new);
    }
}

Let me know if you face any issues.


Thanks,
 
This was selected as the best answer
M SreekanthM Sreekanth
Inside of Helper Code whatever I used for the code is that best practices in Salesforce??

Thanks Sai for your reply 🙂
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sreekanth,

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,
M SreekanthM Sreekanth
Thanks a lot.