The error, 'Aggregate query does not support queryMore(), use LIMIT to restrict the results to a single batch' is in Batch Apex caused because it doesn't support queryMore(). In order to avoid the error, use either of the 2 options.
NOTE: The code provided below are examples.
global class MySchedulableClass implements Schedulable{
@ReadOnly
global void execute (SchedulableContext ctx){
List<AggregateResult> query = [Select MIN(CreatedDate) CD from Account];
DateTime dt;
for(AggregateResult ar : query){
dt = (DateTime)ar.get('CD');
}
RunQuery rq = new RunQuery(dt);
Database.executeBatch(rq);
}
}
global class RunQuery implements Database.Batchable<sObject> {
global DateTime dt;
global RunQuery(DateTime dt){
this.dt = dt;
}
global Database.QueryLocator start(Database.BatchableContext BC){
System.debug('const' + dt);
Set<DateTime> dtime = new Set<DateTime>();
dtime.add(dt);
DateTime createdDate;
String query;
query = 'select Id from Account where CreatedDate =: dt';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
for(sObject s : scope){
System.debug('----------' + s);
}
}
global void finish(Database.BatchableContext BC){
AsyncApexJob a =
[Select Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems,CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
System.debug('********: ' + a.Id);
}
}
global class AggregateResultIterable implements Iterable<AggregateResult>{
global Iterator<AggregateResult> Iterator(){
return new AggregateResultIterator();
}
}
global class AggregateResultIterator Implements Iterator<AggregateResult>{
AggregateResult [] results {get;set;}
Integer index {get;set;}
global AggregateResultIterator(){
String query = 'select Id, MIN(createdDate) from Account GROUP BY Id LIMIT 1';
results = Database.query(query);
}
global boolean hasNext(){
return results !=null && !results.isEmpty() && index < results.size();
}
global AggregateResult next(){
return results[index++];
}
}
Batch Class
global class RunQuery implements Database.Batchable<AggregateResult> {
global Iterable<AggregateResult> start(Database.BatchableContext BC){
return new AggregateResultIterable();
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
for(sObject s : scope){
System.debug('----------' + s);
}
}
global void finish(Database.BatchableContext BC){
AsyncApexJob a =
[Select Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems,CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
System.debug('********: ' + a.Id);
}
}
000386516

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.