5 answers
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; }}