Posted By

abhinavguptas on 09/09/10


Tagged

batch Apex


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Batch Apex Database.Batchable AggregateResult via Iterable


 / Published in: C#
 

  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
Posted By: jharris344 on December 9, 2010

Ho do you test this? I created a class almost like this but when I test it based on what Salesforce posted for batch apex I only get 15% coverage. The coverage does not include this method:

execute(Database.BatchableContext BC, List scope)

Thanks.

You need to login to post a comment.