Batch Apex Database.Batchable AggregateResult via Iterable


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. global class IterableAggregator implements Database.Batchable<AggregateResult> {
  2.  
  3. global Iterable<AggregateResult> start(Database.batchableContext info){
  4. // just instantiate the new iterable here and return
  5. return new AggregateResultIterable();
  6. }
  7.  
  8. global void execute(Database.BatchableContext BC, List<Sobject> scope){
  9. System.debug('SCOPE > ' + scope);
  10. }
  11.  
  12. global void finish(Database.BatchableContext BC){
  13. }
  14.  
  15. global class AggregateResultIterable implements Iterable<AggregateResult> {
  16. global Iterator<AggregateResult> Iterator(){
  17. return new AggregateResultIterator();
  18. }
  19. }
  20.  
  21. global class AggregateResultIterator implements Iterator<AggregateResult> {
  22. AggregateResult [] results {get;set;}
  23. Integer index {get; set;}
  24.  
  25. global AggregateResultIterator() {
  26. index = 0;
  27. String query = 'Select Department, COUNT(Name) From Contact GROUP BY Department';
  28. results = Database.query(query);
  29. }
  30.  
  31. global boolean hasNext(){
  32. return results != null && !results.isEmpty() && index < results.size();
  33. }
  34.  
  35. global AggregateResult next(){
  36. return results[index++];
  37. }
  38. }
  39.  
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.