0

Unlike OP in this question, I am using a custom iterator and I'm getting the same error as stated in the title.

This is my first attempt of writing a custom iterator and I'm having troubles compiling the code, please bear with me.

My goal is to pass a list of strings to a batch, and process each one individually.

This is what I have so far:

My Class

String a ='test';
string b = 'test2';
List<String> jsonStrings;
jsonStrings.add(a);
jsonStrings.add(b);
Database.executeBatch(new CustomBatch(jsonStrings),1);

CustomBatch

global class CustomBatch implements Database.batchable<SObject>{ 

   global Iterable<String> start(Database.batchableContext info,List<SObject> jsonStrings){ 
       return new CustomIterable(jsonStrings); 
   }     
   global void execute(Database.batchableContext info, List<String> scope){ 
       List<String> jsonsToUpdate = new List<String>(); 
       Integer i = 0;
       for(String a : scope){ 
           System.debug(loggingLevel.Error, '*** json_'+i+': '+ + a); 
           i++;
       } 
   }     
   global void finish(Database.batchableContext info){     
   } 
}

CustomIterable:

global class CustomIterable implements Iterable<String>,Iterator<String>{ 

   List<String> jsons {get; set;} 
   Integer i {get; set;} 
   public Iterator<String> iterator() { return this; }

   public CustomIterable(List<String> jsonStrings){ 
       jsons=jsonStrings; 
       i=0;
   }   

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

   global String next(){ 
       i++; 
       return jsons[i-1]; 
   } 
}

Errors while trying to save CustomBatch:

Result: [OPERATION FAILED]: classes/CustomBatch.cls: Class CustomBatch must implement the method: System.Iterable Database.Batchable.start(Database.BatchableContext) (Line: 1, Column: 14)

classes/CustomBatch.cls: Class CustomBatch must implement the method: void Database.Batchable.execute(Database.BatchableContext, List) (Line: 1, Column: 14)

  • Please read this link: andyinthecloud.com/2013/08/11/… to understand how to implement iterable. – Pragati Jain Oct 18 '18 at 8:51
  • 2
    Iterable<String> is not the same as Iterable<SObject>. If you change your batch class to implement Batchable<Object> or even Bachtable<String> it'll work. Note that you also have to change your start(), because that should only get 1 parameter, the BatchableContext. – rael_kid Oct 18 '18 at 8:51
1

Note that List<String> implements Iterable<String> so you don't have to write a class for that.

Something like this is closer to what you are looking for:

public class CustomBatch implements Database.Batchable<String> {

   private List<String> jsonStrings;

   public CustomBatch(List<String> jsonStrings) {
       this.jsonStrings = jsonStrings;
   }

   public List<String> start(Database.BatchableContext info) { 
       return jsonStrings; 
   }

   public void execute(Database.BatchableContext info, List<String> scope) { 
       Integer i = 0;
       for(String a : scope){ 
           System.debug(loggingLevel.Error, '*** json_' + i + ': ' + a); 
           i++;
       } 
   }

   public void finish(Database.BatchableContext info) {     
   } 
}
  • Hi @Keith C quick question, as per my question I have a list of let's say 1000 records, when I call the batch using this line -> Database.executeBatch(newCustomBatch(jsonStrings),1); Will each record be processed seperatly in the batch call? does it know each time to send a record which wasn't been processed yet? – Json Nov 7 '18 at 11:59
  • @Json Yes if you set the batch size to 1 it will be 1 record at a time. – Keith C Nov 7 '18 at 13:17
  • thanks @Keith C, so Salesforce knows to take only one record from the list and process it? and then in the next iteration it goes to the next element in the list? I'm a bit skeptic so would really appreciate your confirmation – Json Nov 7 '18 at 13:49
  • @json Yes the query in the start method is executed and then one row fed through at a time to the execute method. – Keith C Nov 7 '18 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.