You need to sign in to do that
Don't have an account?
Dbjensen
How to bulkify this class (adding Lead ID to custom object)
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;
}
}
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;
}
}
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;
}
}
All Answers
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;
}
}