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
Janna McNeilJanna McNeil 

Developer Script Exception - Too many query rows

I am working to troubleshoot a batch Apex job that runs on a schedule but returns errors. I've read that I need to batch some of the queries but I have no idea how to do that. The Apex Job runs a batch update of all of our accounts from a couple of different custom objects. Any assistance or a point in the right direction would be greatly appreciated since I inherited this code. Thank you.

global class BatchUpdateAccounts implements Database.Batchable<sObject>, Database.Stateful {
      global final String query;
      global final Date inputDate;
      global Date maximumDate;
     
     
      global BatchUpdateAccounts(String q, Date d){
            query = q; 
            inputDate = d;
      }
     
       global Database.Querylocator start(Database.BatchableContext bc) {
            try {
                  Date d = Date.today();
                  Date targetDate;
                  if(d.month() == 1 || d.month() == 2 || d.month() == 3) {
                  targetDate = Date.newInstance(d.year(), 1, 1);
            }
            else if(d.month() == 4 || d.month() == 5 || d.month() == 6) {
                  targetDate = Date.newInstance(d.year(), 4, 1);
            }
            else if(d.month() == 7 || d.month() == 8 || d.month() == 9) {
                  targetDate = Date.newInstance(d.year(), 7, 1);
            }
            else {
                  targetDate = Date.newInstance(d.year(), 10, 1);
            }

Only a portion of the code is included here.
scottbcovertscottbcovert
In the System.scheduleBatch there is an optional scope parameter, which can be used to specify the # of records passed into the execute method-reducing it can help avoid hitting governor limits like the one you're seeing. Try setting this to a lower value and see if that helps. Here is some helpful documentation, scroll down to the 'Using the System.scheduleBatch Method' section for more information:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm