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
Sai LaasyaSai Laasya 

Need help with Inserting records into Custom Object on creation of Lead using Data Loader

I have a scenario, where I need to insert data into Lead object using Data Loader by checking for the duplicates based on the Email field. Inserting data and preventing duplicate fields has been done with import wizard.

But here, while importing data into Lead we also need to save all the rejected/duplicate records into a Custom Object. I have created a before Insert trigger to prevent duplicates and to save the records into Custom Object. But, custom Object records are not populated.
Any suggestions, where  I am going wrong.

trigger findduplicates on Lead (before insert) {
Map<String, Lead> leadMap = new Map<String, Lead>();
  List<Duplicate_Lead__c> dups = new List<Duplicate_Lead__c>();
    for (Lead lead : System.Trigger.new) {
       
        // Make sure we don't treat an email address that 
        // isn't changing during an update as a duplicate. 
   
        if ((lead.Email != null) &&
                (System.Trigger.isInsert ||
                (lead.Email !=
                    System.Trigger.oldMap.get(lead.Id).Email))) {
       
            // Make sure another new lead isn't also a duplicate 
   
            if (leadMap.containsKey(lead.Email)) {
              lead.Email.addError('Another new lead has the same email address.');
                  system.debug('Another new lead has the same email address.');
                 
       Duplicate_Lead__c dup= new Duplicate_Lead__c ();
       dup.First_Name__c = lead.FirstName;
       dup.Last_Name__c = lead.LastName;
       dup.Email__c = lead.Email;      
      
       dups.add(dup);                
 
                                                    
      }else{
                leadMap.put(lead.Email, lead);
            }
       }
     
    }
     insert dups;
}

Thanks in Advance
SravsSravs
When ever trigger fires addError() method it will stop the process (behaves like exception occurence) and it will not execute the rest of the statements. You need to change the code in such a way that trigger call a future method to create and insert  the dups before using addError() method. 
Sai LaasyaSai Laasya
Hi Sravan

Thank you for replying.

As per your suggestion, I have created a method using @future as follows:

@future
public void ins_rec(List<custom_obj__c> recs)
{

}

But the method doesn't accept list arguements when we use @future

Can you please guide me through this.
Grazitti TeamGrazitti Team
Hi Sai,
  • I dont know how this exactly works to check duplicate without comparing other Lead records in the databse.
  • Also, How you are using trigger.oldmap in the before inser trigger.
However, You can not- 
1. Inser the record in such trigger where you are adding error 
2. can not excute scheduler
3. Can not excute future method


The only record of the error is in the system log. find here such other discussions-
https://developer.salesforce.com/forums/ForumsMain?id=906F000000090AuIAI
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000926FIAQ

You need to find out another workaround. Please let me know if you need any help.

Mark this answer as best it helps you!

Thanks!!