Skip to main content
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
5 answers
  1. Nov 10, 2020, 6:55 PM
    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;

        }

    }
0/9000