+ Start a Discussion

pull duplicate records into custom object using trigger


I just done logic to identify duplicates leads based on email address and if any duplicate record is find then adding object .add error and avoiding duplicate record creation into database.
but user is going to upload bulk records so what ever i found duplicates i have to place those records into Custom object so when i use this adderror in trigger it is not allowing to create records. and with in the debug log it showing some records insertion successfully.

how to use adderror and duplicates i have to place into custom object.

trigger LeadDupeCheckTrigger on Lead (before insert, before update) {
for(Lead l:trigger.new){
  if(l.email =='test@sfdc.com'){
     l.addError('this is duplicate lead ');
   // Trying to place into custom object
Duplicate_Leads__c Dl = new Duplicate_Leads__c();
      Dl.First_Name__c = newLead.firstname;
      Dl.Last_Name__c = newLead.lastname;
      Dl.Company__c = newLead.company;
      Dl.Email__c = newLead.email;   

// finally inserting into custom object

     insert lstDLeads;

kaustav goswamikaustav goswami
I think the actual problem is if you use the addError method Salesforce treats it as an error and does not allow the DML operations involved in the transaction to be committed. Rather all changes are rolled back. Apex transactions in Salesforce are atomic in nature.

That is why the records are not going into the Duplicate Lead object.