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
niraj kumar 45niraj kumar 45 

Too many query rows: 50001 in batch class

I am getting System.LimitException: Too many query rows: 50001 at line wher I am Iterating  while (it.hasNext()).
I have user records more than 50000 so I am using querylocator but when I am iterating I am getting Too many query rows exception.
Need help regarding this.
below is my code.:(the below code is in execute method of the batch class)

            string allUserQuery = 'Select Id,Name from User';
            Database.QueryLocator q = Database.getQueryLocator(allUserQuery);
            Database.QueryLocatorIterator it =  q.iterator();
            
            // Iterate over the records
            while (it.hasNext())
            {
               UserRec.add((User)it.next());
            
            }
NagendraNagendra (Salesforce Developers) 
Hi Niraj,

Please check with below post from stack exchange community with same issue while using batch apex.This might be helpful for you to accelerate with the above requirement. Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue and becomes a proper solution which results in helping others who are really in need of it.​

Best Regards,
Nagendra.P