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
Teja SattoorTeja Sattoor 

ERROR: CreateAccountForContact: System.LimitException: Too many query rows: 50001

Hi,

We recently made the change to work from contacts instead of leads in Salesforce. We get all our contacts from our marketing generation tool without an Account. The company name of these contacts is stored in a field called "Company__c". I wrote a code that looks if the contact is from our existing customer (Zlien_User_ID__c) and associates with the same Salesforce Account(User_ID__c), if not it will look for the matching Account name and associates with it, and if not finally it will create an account and associate with it. The following code works fine when one contact is created by the lead gen tool at a time. Sometimes, we may have to bulk upload contacts using dataloader which is when I encounter the error message: System.LimitException: Too many query rows: 50001. 

Please find the Code below:

public class assignAccount
{
    public static void assignAccounts(Set<Id> contactIds) 
    {

        Map<String,Account> matchUserIdMap = new Map<String,Account>();
        Map<String,Account> matchCompanyMap = new Map<String,Account>();
        
        for(Account acc : [Select User_ID__c, name, Id from Account])
        {
            if(acc.User_ID__c != null)
            {
                // Contact map when Zlien user id is not null.
                matchUserIdMap.put(acc.User_ID__c, acc);
            }
            else
            {
                // Contact map when Zlien user id is null.
                matchCompanyMap.put(acc.name, acc);
            }
            
        }
       
        for(Contact con : [Select Zlien_User_ID__c, Company__c, accountId, ownerId from Contact where Id IN :contactIds])
        {
            if(con.Zlien_User_ID__c != null)
            {
                if(matchUserIdMap.containsKey(con.Zlien_User_ID__c))
                {
                   con.accountId = matchUserIdMap.get(con.Zlien_User_ID__c).Id;
                }
                
                else if(matchCompanyMap.containsKey(con.Company__c))
                {
                    con.accountId = matchCompanyMap.get(con.Company__c).Id;
                }
                
                else
                {
                    Account A = new Account(name = con.Company__c, ownerId = con.ownerId);
                    insert A;
                    con.accountId = A.Id;
                }
            }
            else if(con.Company__c != null)
            {
                if(matchCompanyMap.containsKey(con.Company__c))
                {
                    con.accountId = matchCompanyMap.get(con.Company__c).Id;
                }
                
                else
                {
                    Account A = new Account(name = con.Company__c, ownerId = con.ownerId);
                    insert A;
                    con.accountId = A.Id;
                }
            }
            
            update con;          
        }
         
    }
    
}

Thanks in Advance,
Teja
Best Answer chosen by Teja Sattoor
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Use filter in the for(Account acc : [Select User_ID__c, name, Id from Account])

 

All Answers

Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Use filter in the for(Account acc : [Select User_ID__c, name, Id from Account])

 

This was selected as the best answer
Teja SattoorTeja Sattoor
Hi Bhaswanthnaga vivek vutukuri,

Thanks, I did use it and was able to figure it out. Used sets to get the contact fields and added those to the accounts filter.

Teja.