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
Kajal Singh 36Kajal Singh 36 

I written a two different trigger for single object and i want to write in a single handler and trigger. How can i do this

This trigger is to prevent for duplication...
trigger LeadDuplicate on Lead__c (before insert) {
   List<Lead__c> accList=new List<Lead__c>([select Email__c, Name__c, Phone_No__c from Lead__c]);
    map<String,Lead__c> accmap=new map<String,Lead__c>();
    for(Lead__c acc:accList){
        accmap.put(acc.Email__c,acc);
        accmap.put(acc.Phone_No__c,acc);
        
    }
     
    
    //For Insert Operation
    if(Trigger.isinsert&&Trigger.isbefore){
        for(Lead__c acc:Trigger.new){
            if(accmap.get(acc.Email__c)!=null || accmap.get(acc.Phone_No__c)!=null){
                acc.adderror('Duplicate Lead exists');
            }
        }
    }
}
-------------------------------------------------------------------------------

This trigger is for copy the lead
This is the handler Class
public class LeadTriggerHelper {
        public static void insertLead(List <Lead> leadList) {
            List<Lead__c> newLeadsList = new List<Lead__c>();
            for(Lead ld: leadlist) {
                Lead__c newLead = new Lead__c();
                newLead.Name__c = ld.LastName;
                newLead.Phone_No__c = ld.Phone;
                newLead.Email__c = ld.Email;
                 newLead.City__c = ld.City;
                newLead.State__c = ld.State;
                newLead.Lead_Status__c = ld.Status;
                newLead.Company__c = ld.Company; 
                newLead.Pincode__c = ld.PostalCode;
                newLead.Country__c = ld.Country;
                newLead.OwnerId = ld.OwnerId;
                newLeadsList.add(newLead);        
            }
            insert newLeadsList;
        }
    }

Trigger
trigger ObjTrigger on Lead (after insert) {
  LeadTriggerHelper.insertLead(Trigger.new);
    
}
--------------------------------------------------------
How can i write this in a single handler and a single trigger
RituSharmaRituSharma
You may create a new trigger with below code and disable/delete the existing ones. You may use one of the existing and disable/delete the other one. Idea is that trigger name should be generic.

trigger LeadTrigger on Lead__c (before insert,after insert) {
    if(trigger.isInsert && Trigger.isbefore) {
        List<Lead__c> accList=new List<Lead__c>([select Email__c, Name__c, Phone_No__c from Lead__c]);
        map<String,Lead__c> accmap=new map<String,Lead__c>();
        for(Lead__c acc:accList){
            accmap.put(acc.Email__c,acc);
            accmap.put(acc.Phone_No__c,acc);            
        }         
        
        for(Lead__c acc:Trigger.new){
            if(accmap.get(acc.Email__c)!=null || accmap.get(acc.Phone_No__c)!=null){
                acc.adderror('Duplicate Lead exists');
            }
        }
    } else if(trigger.isInsert && Trigger.isAfter) {
        LeadTriggerHelper.insertLead(Trigger.new);
    }
}
 
Kajal Singh 36Kajal Singh 36
I want a handler class also.
Kajal Singh 36Kajal Singh 36
Only logics are in trigger and rest of the code in Handler class
RituSharmaRituSharma
You may try as mentioned below:

trigger LeadTrigger on Lead__c (before insert,after insert) {
    if(trigger.isInsert && Trigger.isbefore) {
        LeadTriggerHelper.checkForDuplicates(Trigger.new);
    } else if(trigger.isInsert && Trigger.isAfter) {
        LeadTriggerHelper.insertLead(Trigger.new);
    }
}

---------------------------------------------------------------------------------------------

public class LeadTriggerHelper {
    public static void checkForDuplicates(List<Lead__c> leadList) {
        List<Lead__c> accList=new List<Lead__c>([select Email__c, Name__c, Phone_No__c from Lead__c]);
        map<String,Lead__c> accmap=new map<String,Lead__c>();
        for(Lead__c acc:accList){
            accmap.put(acc.Email__c,acc);
            accmap.put(acc.Phone_No__c,acc);            
        }         
        
        for(Lead__c acc:leadList){
            if(accmap.get(acc.Email__c)!=null || accmap.get(acc.Phone_No__c)!=null){
                acc.adderror('Duplicate Lead exists');
            }
        }
    }

    public static void insertLead(List <Lead> leadList) {
        List<Lead__c> newLeadsList = new List<Lead__c>();
        for(Lead ld: leadlist) {
            Lead__c newLead = new Lead__c();
            newLead.Name__c = ld.LastName;
            newLead.Phone_No__c = ld.Phone;
            newLead.Email__c = ld.Email;
            newLead.City__c = ld.City;
            newLead.State__c = ld.State;
            newLead.Lead_Status__c = ld.Status;
            newLead.Company__c = ld.Company; 
            newLead.Pincode__c = ld.PostalCode;
            newLead.Country__c = ld.Country;
            newLead.OwnerId = ld.OwnerId;
            newLeadsList.add(newLead);        
        }
        insert newLeadsList;
    }
}
RituSharmaRituSharma
You may try as mentioned below:

trigger LeadTrigger on Lead__c (before insert,after insert) {
    if(trigger.isInsert && Trigger.isbefore) {
        LeadTriggerHelper.checkForDuplicates(Trigger.new);
    } else if(trigger.isInsert && Trigger.isAfter) {
        LeadTriggerHelper.insertLead(Trigger.new);
    }
}

---------------------------------------------------------------------------------------------

public class LeadTriggerHelper {
    public static void checkForDuplicates(List<Lead__c> leadList) {
        List<Lead__c> accList=new List<Lead__c>([select Email__c, Name__c, Phone_No__c from Lead__c]);
        map<String,Lead__c> accmap=new map<String,Lead__c>();
        for(Lead__c acc:accList){
            accmap.put(acc.Email__c,acc);
            accmap.put(acc.Phone_No__c,acc);            
        }         
        
        for(Lead__c acc:leadList){
            if(accmap.get(acc.Email__c)!=null || accmap.get(acc.Phone_No__c)!=null){
                acc.adderror('Duplicate Lead exists');
            }
        }
    }

    public static void insertLead(List <Lead> leadList) {
        List<Lead__c> newLeadsList = new List<Lead__c>();
        for(Lead ld: leadlist) {
            Lead__c newLead = new Lead__c();
            newLead.Name__c = ld.LastName;
            newLead.Phone_No__c = ld.Phone;
            newLead.Email__c = ld.Email;
            newLead.City__c = ld.City;
            newLead.State__c = ld.State;
            newLead.Lead_Status__c = ld.Status;
            newLead.Company__c = ld.Company; 
            newLead.Pincode__c = ld.PostalCode;
            newLead.Country__c = ld.Country;
            newLead.OwnerId = ld.OwnerId;
            newLeadsList.add(newLead);        
        }
        insert newLeadsList;
    }
}