I am using an iterator to use an aggregate query on a custom object looking for unique years. on the very last iteration, I want to change a value, so I am hoping to use hasNext, but it looks like i only have the scope, which is a list, and the batchable context. I dont quite see how to call the iterator while in the execute portion of the batch file

private TokenResponse tokenResponse;

global Iterable<AggregateResult> start(Database.BatchableContext bc) {
    tokenResponse = API_Interface.getToken();

    return new RST_TitleYearIterable(
        'SELECT Calendar_Year(Wide_Release_Date__c) openYear, count(Id) num FROM Title__c GROUP BY Calendar_Year(Wide_Release_Date__c)'

global void execute(Database.BatchableContext bc, List<SObject> scope){
    system.debug('scope : ' + scope);
    for(sObject sObj : scope) { // there should be only one
        AggregateResult ar = (AggregateResult)sObj;
        System.debug('>>>> COUNT : ' + ar.get('num') + ' during >>>> YEAR : ' + ar.get('openYear'));
       // check if iterator has next??

Could I store the iterable as a private variable and access it and its current index?

  • you don't want to do this in the execute() as if the iterable is empty, the start method will execute and then pass control in the next transaction to the finish method - execute() will never be called. So, an option is to use database.stateful and then do the final adjustment to the last known iterable in finish()
    – cropredy
    Nov 15, 2018 at 3:42
  • I dont follow. The execute method is firing, and I am using stateful. I was asking if I can access the iterable from within the execute. I'm guessing your saying I cant and I need to modify the result set in the iterable
    – textual
    Nov 15, 2018 at 4:44


You must log in to answer this question.

Browse other questions tagged .