You need to sign in to do that
Don't have an account?
Dbjensen
How to bulify code
Hello - I need to bulkify this code as it's hitting govenor limits. This code triggers when a lead is inserted. The code looks for an existing account and contact based on a match from 3 fields on the lead. If the account and contact are found, it adds the account ID and contact ID to the lead that's being inserted.
Could you please help if you know how to fix this code so it can handle bulk inserts? Thanks for any help you can provide.
public class AddExistingAccountAndContactToLead {
public static void findExistingAccountContact(List<Lead> nonDupe) {
List<Lead> leadsList = new List<Lead>();
List<Id> acctId = new List<Id>();
List<Account> accountList = new List<Account>();
for(Lead newLead : nonDupe) {
if(newLead.Duplicate__c == false) {
List<Account> exsistingAccount = [Select Id FROM Account
Where
First_3_Char_of_Street__c = :newLead.First_3_Char_of_Street__c
AND First_5_Char_of_Last_Name__c = :newLead.First_5_Char_of_Last_Name__c
AND BillingPostalCode = :newLead.PostalCode];
for(Account acct : exsistingAccount) {
newLead.Household__c = acct.Id;
}
}
}
for(Lead newLead : nonDupe) {
if(newLead.Duplicate__c == false) {
List<Contact> existingContact = [Select Id FROM Contact
Where
First_3_Char_of_Street__c = :newLead.First_3_Char_of_Street__c
AND First_5_Char_of_Last_Name__c = :newLead.First_5_Char_of_Last_Name__c
AND MailingPostalCode = :newLead.PostalCode];
for(Contact ct : existingContact) {
newLead.Individual_Client__c = ct.Id;
}
}
}
}
}
Could you please help if you know how to fix this code so it can handle bulk inserts? Thanks for any help you can provide.
public class AddExistingAccountAndContactToLead {
public static void findExistingAccountContact(List<Lead> nonDupe) {
List<Lead> leadsList = new List<Lead>();
List<Id> acctId = new List<Id>();
List<Account> accountList = new List<Account>();
for(Lead newLead : nonDupe) {
if(newLead.Duplicate__c == false) {
List<Account> exsistingAccount = [Select Id FROM Account
Where
First_3_Char_of_Street__c = :newLead.First_3_Char_of_Street__c
AND First_5_Char_of_Last_Name__c = :newLead.First_5_Char_of_Last_Name__c
AND BillingPostalCode = :newLead.PostalCode];
for(Account acct : exsistingAccount) {
newLead.Household__c = acct.Id;
}
}
}
for(Lead newLead : nonDupe) {
if(newLead.Duplicate__c == false) {
List<Contact> existingContact = [Select Id FROM Contact
Where
First_3_Char_of_Street__c = :newLead.First_3_Char_of_Street__c
AND First_5_Char_of_Last_Name__c = :newLead.First_5_Char_of_Last_Name__c
AND MailingPostalCode = :newLead.PostalCode];
for(Contact ct : existingContact) {
newLead.Individual_Client__c = ct.Id;
}
}
}
}
}
Please check once below code :
Hope it helps you.
Thanks
Varaprasad
All Answers
Please check once below code :
Hope it helps you.
Thanks
Varaprasad