+ Start a Discussion
Tulasiram ChippalaTulasiram Chippala 

How to create another object after showing addError on lead

Hi i am using below trigger:
My requirement is like : When i am inserting Lead in bulk
1. if Found and duplicate in contact
2. if Found any duplicate in Lead

if found any duplicates either in Contact or Lead then insert Non_Approved_Lead . I am using below code
trigger compareMultipleFieldLead on Lead (before insert) {
    List<Non_Approved_Leads__c> nonAplFinal = new List<Non_Approved_Leads__c>();
    List<Lead> errorLeadList = new List<Lead>();
    set<string> newEmailSet = new set<string>();
    
    
    set<WrapperClassWrapperClass> dbWrapperSet = new set<WrapperClassWrapperClass>();
    set<WrapperClassWrapperClass> newWrapperSet = new set<WrapperClassWrapperClass>();
    
    Map<String, String> mapLeadList = new Map<String, String>();
    List<Lead> leadInsertBeforeContactCheck = new List<Lead>();
    List<Lead> leadRejectList = new List<Lead>();
    List<Lead> listDumpLeads = new List<Lead>();
    
    set<string> emailSetForContactCheck = new set<string>();
    List<Lead> leadInsertAfterContactCheck = new List<Lead>();
    /* ********************************handling New records *************************************************** */
    if(trigger.isBefore && trigger.isInsert){
        for(lead ldRecord : trigger.new){
            
            WrapperClassWrapperClass iKey = new WrapperClassWrapperClass(ldRecord.Email, ldRecord.ownerid);
            
            if(newWrapperSet.contains(iKey)){
                system.debug('Found Duplicate Record'+ldRecord);
                leadRejectList.add(ldRecord);
                
            } else {
                listDumpLeads.add(ldRecord);
                newEmailSet.add(ldRecord.Email);
                newWrapperSet.add(iKey);
                //owneridSet.add(ownerid);
                system.debug('ikey is  --'+iKey);
            }
        }
        /* ********************************handling old records *************************************************** */
        for(Lead dbLead : [select id, LastName, email, phone, company, ownerid from lead where email IN: newEmailSet]){
            system.debug('came to query');
            dbWrapperSet.add(new WrapperClassWrapperClass(dbLead.Email, dbLead.ownerid));
        }
        for(lead ldRecord : listDumpLeads){
            system.debug('came to second loop iterate ove new list');
            
            WrapperClassWrapperClass iKey = new WrapperClassWrapperClass(ldRecord.Email, ldRecord.ownerid);
            system.debug('ikey is  --'+iKey);
            
            if(dbWrapperSet.contains(iKey)) {
                leadRejectList.add(ldRecord);
            }
            
            else {
                // mapLeadList.put(ldRecord.Email, ldRecord);
                emailSetForContactCheck.add(ldRecord.Email);
                leadInsertBeforeContactCheck.add(ldRecord);   
            }
            
        }
    }  
    system.debug('-----email list for duplicates'+emailSetForContactCheck.size());
    system.debug('lead Reject List after check in Leads'+leadRejectList.size());
    // Handling Contacts from lead filterd dump list
    List<Contact> consList;
    Set<String> emailSetContact=new Set<String>();
    if(emailSetForContactCheck.size()>0){
        consList = [select id, email from contact where email IN:emailSetForContactCheck];
        if(consList.size()>0){
        for(Contact con : consList ){
            emailSetContact.add(con.email);
            mapLeadList.put(con.email, con.Id);
        }
    }
    }
    system.debug('list lead dumps'+listDumpLeads.size());
    for(Lead ld:listDumpLeads){
        if(emailSetContact.size()>0 && emailSetContact != null && emailSetContact.contains(ld.Email)){
            system.debug('which contact contacins this email id'+mapLeadList.get(ld.email));
            leadRejectList.add(ld);
        }else {
            leadInsertAfterContactCheck.add(ld);
        }
    }   
    system.debug('lead Insert Before Contact Check'+leadInsertBeforeContactCheck.size());
    system.debug('lead Reject List after contact check also'+leadRejectList.size());
    system.debug('Final Lead List'+leadInsertAfterContactCheck.size());
    /* ******************************************* Creating Non Approved Leads for Duplicate Leads ******************************************** */     
    for(Lead ldRecord:leadInsertAfterContactCheck){
        Non_Approved_Leads__c nonApl = new Non_Approved_Leads__c(name = ldRecord.FirstName+' '+ldRecord.LastName,
                                                                 Email__c = ldRecord.Email,
                                                                 Phone__c = ldRecord.Phone,
                                                                 Direct_Phone__c = ldRecord.Direct_Phone__c,
                                                                 Lead_Contact_Job_Title__c = ldRecord.Lead_Contact_Job_Title__c,
                                                                 Lead_LinkedIn_Profile__c = ldRecord.Lead_LinkedIn_Profile__c,
                                                                 Personal_Email__c = ldRecord.Personal_Email__c,
                                                                 Comments__c = ldRecord.Comments__c,
                                                                 Website__c = ldRecord.Website,
                                                                 Title__c = ldRecord.Title,
                                                                 Status__c = ldRecord.Status, 
                                                                 Industry__c = ldRecord.Industry,
                                                                 Description__c = ldRecord.Additional_Contact_Information__c, 
                                                                 Company__c = ldRecord.Company, 
                                                                 Street__c = ldRecord.Street,
                                                                 City__c = ldRecord.City,
                                                                 State__c = ldRecord.State,
                                                                 Zip_Code__c = ldRecord.PostalCode, 
                                                                 Country__c = ldRecord.Country,
                                                                 ownerid = ldRecord.ownerid
                                                                );
        nonAplFinal.add(nonApl);
    }
    doCalloutFromFuture.callFromDupeTrigger(nonAplFinal);
    //insert nonAplFinal; // Inserting non approve Leads 
    //system.debug('-------insert non approved leads'+nonAplFinal);
    /* ******************************************* showing add Error for final Dupe List ******************************************** */ 
        for(Lead ld:leadRejectList){
            System.debug('contained duplicate trigger');
            ld.addError('This lead is existed');
    }
}

The error is, if it found any duplicate error it is showing addError message and not able to found inserted Non_Approved_Leads
I checked logs Non_Approved_Leads id's are showing but not able to find it. Please help me on this.