• Ankit Rustagi 19
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

I'm trying to write a Batchable that uses an Iterable.  I'm getting one error.  The example in the documentation isn't much help as it declares the Iterable without a type and I can't even get that to compile.

 

The issue in in the assignment of myIter in the start() method.  I get a runtime exception on that line. 

 

global class KenTestBatch implements Database.batchable<Transaction__c> { 
   /*
   	KenTestBatch batcher = new KenTestBatch();
    	Id procId = Database.executeBatch(batcher,1);
    	System.debug('ProcId ' + procId);
   */
   global Iterable<Transaction__c> start(Database.BatchableContext info){
   		Iterable<Transaction__c>myIter=(Iterable<Transaction__c>)new KenTestCustomIterable(); 
       return  myIter;
   }     

   global void execute(Database.BatchableContext info, List<Transaction__c> scope){

       for (Transaction__c tran : scope) { 

		// Do processing here.

       } 
   }     

  global void finish(Database.BatchableContext info){     
  } 
}

global class KenTestCustomIterable implements Iterator<Transaction__c>{ 

   List<Transaction__c> tranList {get; set;} 
   Integer i {get; set;} 

   public KenTestCustomIterable() { 
       tranList =        [SELECT Id, Back_Office_Order__c
       						FROM Transaction__c 
       						limit 100]; 
       i = 0; 
   }   

   global boolean hasNext(){ 
       if(i >= tranList.size()) {
           return false; 
       } else {
           return true; 
       }
   }    

   global Transaction__c next(){ 
       i++; 
       return tranList[i-1]; 
   } 
}