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)
Iterable<String>
is not the same asIterable<SObject>
. If you change your batch class to implementBatchable<Object>
or evenBachtable<String>
it'll work. Note that you also have to change yourstart()
, because that should only get 1 parameter, the BatchableContext.