Apexバッチで集計関数
//バッチクラス
global class SampleAggregateBatch implements Database.Batchable<AggregateResult> {
// The batch job starts
global Iterable<AggregateResult> start(Database.BatchableContext bc){
String query = 'SELECT COUNT(Id) cnt, AccountId FROM Contact GROUP BY AccountId';
return new SearchResultIterable(query);
}
// The batch job executes and operates on one batch of records
global void execute(Database.BatchableContext bc, List<sObject> scope){
for(sObject sObj : scope) {
AggregateResult ar = (AggregateResult)sObj;
System.debug('>>>> COUNT : ' + ar.get('cnt'));
}
}
// The batch job finishes
global void finish(Database.BatchableContext bc){ }
}
//インタフェースIterable Apex class
public class SearchResultIterable implements Iterable<AggregateResult>{
private String query;
public SearchResultIterable(String soql){
this.query = soql;
}
public Iterator<AggregateResult> Iterator(){
return new SearchResultIterator(this.query);
}
}
//インタフェースIterator
public class SearchResultIterator Implements Iterator<AggregateResult>{
public AggregateResult [] results {get;set;}
public Integer index {get;set;}
public SearchResultIterator(String query){
this.index = 0;
results = Database.query(query);
}
public boolean hasNext(){
return results !=null && !results.isEmpty() && index < results.size();
}
public AggregateResult next(){
return results[index++];
}
}
// Run batch apex
SampleAggregateBatch batch = new SampleAggregateBatch();
Database.executebatch(batch, 200);
// Debug log output
>>>> COUNT : 1
>>>> COUNT : 3
>>>> COUNT : 2