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)
Best Answer
Note that
List<String>
implementsIterable<String>
so you don't have to write a class for that.Something like this is closer to what you are looking for: