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
V'NathV'Nath 

pull duplicate records into custom object using trigger

Hi 

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;   
      lstDLeads.add(Dl);
     lstem.add(newLead.email);    

   }
}
// finally inserting into custom object

if(!lstDLeads.isEmpty())
     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.

Thanks,
Kaustav