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
aamDevaamDev 

System.Exception: Too many query rows: 1001

Through some customizations, we have modified the Account object to either have a recordtype of Company or Branch. In the event that an Account has a recordtype of Branch, we populate a custom field named Home_Office__c with the Company's ID (Account ID). As part of a larger contact validation trigger, I am trying to grab the IDs of all the Branches whose Home_Office__c falls within a certain Set. 

 

Here's the excerpt of my trigger where I create a Set of Home_Office__c from the Triggers Account:

 

Set<ID> homeID = new Set<ID>();
				
for (Account a : [SELECT Home_Office__c FROM Account WHERE Id =: accID AND Home_Office__c != null]) {
					
homeID.add(a.Home_Office__c);
					
}

 

Here's the excerpt where I create a Set of Account IDs that fall within the Home_Office__c set:

 

Set<ID> branchID = new Set<ID>();
				
for (Account a : [SELECT Id FROM Account WHERE Home_Office__c IN : homeID]) {
					
branchID.add(a.Id);

}

 

FYI - I split these values over two sets becausing I'm mapping contact names first from the parent company and then from all the branches. 

 

The first set will rarely ever be larger than 1 row, with the branch set reaching a size of around 600. However, it is the branch set that is causing the System.Exception: Too many query rows: 1001.

 

I thought I had thoroughly tested this in our Sandbox, but only when I deployed the trigger to production did I run across this error. I have since updated our Sandbox to reflect records similar to production, and through testing, the only conclusion I can come to is that the second query is grabbing all Account IDs; not just ones with a valid Home_Office__c value.

 

Any idea of why this may be happening? I've avoided including the entire trigger since it all had worked fine before, but if necessary I'll post it. Thanks in advance.

 

Adriel

Ispita_NavatarIspita_Navatar

Your org must be returning large quantum of data , which making your trigger hit the governor's limit, which have been put in place to support multi-tenancy.

Having said that you are point that under no condition will the query return more that  600 records, but it seems it is doing.

Can you try 1 thing can you run your code with by putting a limit say of 900.

 

Set<ID> branchID = new Set<ID>();
                
for (Account a : [SELECT Id FROM Account WHERE Home_Office__c IN : homeID limit 600 ])

{
branchID.add(a.Id);
}

 

and see if you are still encountering the error.

Also please try another thing do replicate the query which is giving the error and run it in the "system log" and see the quantum of data it is returning my assumption is that it must be retuning data in access of 1000 else you will not get the error.

So please do try 2 things:-

1. Run your code with the limit clause just to see if the error is apprearing

2. Try running the query in system log to see if the error is coming if the error comes rerun the query with a limit of limit 999.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

aamDevaamDev

Checking my debug log I found that my query was working fine, but a workflow rule was causing it to run 2x - triggering the governor limit error (I had tried the limit option previous to the post).

 

With or without the workflow rule, it appears that our organization will more than likely have a scenario which reaches the governor limits on my branch query. I must find a way to avoid reaching those limits. I'm looking into batch apex, but any further suggestions would be much appreciated. Thanks again for your help.

 

Adriel