Loading
Salesforce から送信されるメールは、承認済ドメインからのみとなります続きを読む

Apex の一括処理エラー「Aggregate query does not support queryMore (Aggregate クエリが queryMore をサポートしていません)」

公開日: Oct 13, 2022
説明
※この記事は英語版を翻訳しており、一部機械翻訳を含むため内容は後日更新される可能性があります。最新の内容は英語版を参照してください。表示言語は画面右下の言語名から切り替えられます。
 
Aggregate クエリは queryMore() をサポートしていないため、Apex の一括処理で Aggregate クエリは動作しません。「Aggregate query does not support queryMore(), use LIMIT to restrict the results to a single batch (Aggregate クエリは queryMore() をサポートしていないため、LIMIT を使用して結果を 1 つの一括処理に制限してください)」というエラーが発生します。
解決策

Aggregate query does not support queryMore(), use LIMIT to restrict the results to a single batch (集計クエリは queryMore() をサポートしていないため、LIMIT を使用して結果を 1 つの一括処理に制限してください)」というエラーは、Apex の一括処理で queryMore() がサポートされていないために発生します。このエラーを回避するには、2 つのオプションのいずれかを使用します。

注意: 以下のコードはサンプルです。

1.@ReadOnly アノテーションを使ってスケジュールされたクラスで「集計クエリ」を実行し、一括処理クラスを呼び出します。

スケジュールクラス
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);  }  }

2.一括処理クラスではクエリロケータの代わりに Iterable インターフェースを実装します。 

Iterable クラス

global class AggregateResultIterable implements Iterable<AggregateResult>{  global Iterator<AggregateResult> Iterator(){  return new AggregateResultIterator();  }  }
Iterator クラス
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++];  }  }

一括処理クラス

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

 
読み込み中
Salesforce Help | Article