Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
hello - I have class that adds the Lead ID to a custom object. However, it adds the same ID to very lead when I do a bulk upload because of this line: ldsubs.Lead__c = listOfLeadsSubIDs[0];

I can't figure how how to add the corect Lead Id to every lead inserted via bulk insert. Any assistance would be greatly appreciated. 

public class AddLeadSubmissionToLead {

    

    public static void addLStoLead (List<Lead> leadsFromTrigger) {

        

        Set<String> nameCompany = new Set<String>();

        List<Lead_Submission__c> listOfLeadsSubs = new List<Lead_Submission__c>();

        List<Id> listOfLeadsSubIDs = new List<Id>();

        

        for(Lead nwLead : leadsFromTrigger) {

            listOfLeadsSubIDs.add(nwLead.Id);

            nameCompany.add(nwLead.LastNameStreetAndZipCode__c );

        }

        

        List<Lead_Submission__c> ldSub = [SELECT Id, Lead__c, LastNameStreetAndZipCode__c

                                          FROM Lead_Submission__c 

                                          WHERE LastNameStreetAndZipCode__c =:nameCompany];

        

        System.debug('Lead Submission Found ' + ldSub);

        

        for(Lead_Submission__c ldsubs : ldSub) {

            if(ldsubs.Lead__c == null && !ldSub.isEmpty()) {

                ldsubs.Lead__c = listOfLeadsSubIDs[0];

                listOfLeadsSubs.add(ldsubs);

            }

        }

        update listOfLeadsSubs;

    }

}
2 answers
  1. Feb 22, 2018, 8:21 PM
    Check this one. 

    public class AddLeadSubmissionToLead {

        

        public static void addLStoLead (List<Lead> leadsFromTrigger) {

            

            //Set<String> nameCompany = new Set<String>();

            map<string,id> mapLeadCompanies = new map<string,id>();

            List<Lead_Submission__c> listOfLeadsSubs = new List<Lead_Submission__c>();

            List<Id> listOfLeadsSubIDs = new List<Id>();

            

            for(Lead nwLead : leadsFromTrigger) {

                listOfLeadsSubIDs.add(nwLead.Id);

                //nameCompany.add(nwLead.LastNameStreetAndZipCode__c );

                mapLeadCompanies.put(nwLead.LastNameStreetAndZipCode__c,nwLead.id);

            }

            

            List<Lead_Submission__c> ldSub = [SELECT Id, Lead__c, LastNameStreetAndZipCode__c

                                              FROM Lead_Submission__c 

                                              WHERE LastNameStreetAndZipCode__c in:mapLeadCompanies.keyset()];

            

            System.debug('Lead Submission Found ' + ldSub);

            

            for(Lead_Submission__c ldsubs : ldSub) {

                if(ldsubs.Lead__c == null && !ldSub.isEmpty()) {

                    ldsubs.Lead__c = mapLeadCompanies.get(ldsubs.LastNameStreetAndZipCode__c);//listOfLeadsSubIDs[0];

                    listOfLeadsSubs.add(ldsubs);

                }

            }

            update listOfLeadsSubs;

        }

    }
Loading
0/9000