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
DbjensenDbjensen 

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;
                }            
                           
            }

        }

    }
}
Best Answer chosen by Dbjensen
v varaprasadv varaprasad
Hi,

Please check once below code : 
 
List<Lead> leadsList = new List<Lead>();
        List<Id> acctId = new List<Id>();
        List<Account> accountList = new List<Account>();
		
		
		list<string> first3chars = new list<string>();
		list<string> first5chars = new list<string>();
		list<string> billingPostal = new list<string>();
		
		
        
        for(Lead newLead : nonDupe) {
            if(newLead.Duplicate__c == false) {
				first3chars.add(newLead.First_3_Char_of_Street__c);
				first5chars.add(First_5_Char_of_Last_Name__c);
				billingPostal.add(newLead.PostalCode);
            }

        }
		
		 Account exsistingAccount = [Select Id FROM Account
                                               Where 
                                               First_3_Char_of_Street__c in :first3chars
                                               AND First_5_Char_of_Last_Name__c :first5chars
                                               AND BillingPostalCode :billingPostal]; 
                
               
         LContact existingContact = [Select Id FROM Contact
                                               Where 
                                               First_3_Char_of_Street__c in :first3chars
                                               AND First_5_Char_of_Last_Name__c :first5chars
                                               AND MailingPostalCode : billingPostal]; 



											   
			for(Lead newLead : nonDupe) {								   
			   
                    newLead.Household__c = exsistingAccount.Id;
					newLead.Individual_Client__c = existingContact.Id;
                
            }

Hope it helps you.

Thanks
Varaprasad

All Answers

v varaprasadv varaprasad
Hi,

Please check once below code : 
 
List<Lead> leadsList = new List<Lead>();
        List<Id> acctId = new List<Id>();
        List<Account> accountList = new List<Account>();
		
		
		list<string> first3chars = new list<string>();
		list<string> first5chars = new list<string>();
		list<string> billingPostal = new list<string>();
		
		
        
        for(Lead newLead : nonDupe) {
            if(newLead.Duplicate__c == false) {
				first3chars.add(newLead.First_3_Char_of_Street__c);
				first5chars.add(First_5_Char_of_Last_Name__c);
				billingPostal.add(newLead.PostalCode);
            }

        }
		
		 Account exsistingAccount = [Select Id FROM Account
                                               Where 
                                               First_3_Char_of_Street__c in :first3chars
                                               AND First_5_Char_of_Last_Name__c :first5chars
                                               AND BillingPostalCode :billingPostal]; 
                
               
         LContact existingContact = [Select Id FROM Contact
                                               Where 
                                               First_3_Char_of_Street__c in :first3chars
                                               AND First_5_Char_of_Last_Name__c :first5chars
                                               AND MailingPostalCode : billingPostal]; 



											   
			for(Lead newLead : nonDupe) {								   
			   
                    newLead.Household__c = exsistingAccount.Id;
					newLead.Individual_Client__c = existingContact.Id;
                
            }

Hope it helps you.

Thanks
Varaprasad
This was selected as the best answer
DbjensenDbjensen
Hi Varaprasad - Thanks so much for the help. When I insert more than 1 lead, I'm now getting the error "System.QueryException: List has more than 1 row for assignment to SObject()". Do you know how I can get around this?
DbjensenDbjensen
Ok, I figured it out. I added the results to a map and assigned the ids from that. Thanks again for the help. It certainly got me to the correct code.