function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
glorgeglorge 

Apex Documentation on Custom Iterators make my brain hurt

Looking at the Apex documentation for Custom Iterators:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_iterable.htm#kanchor338

 

 

Code samples could use a bit of work:

 

IterableString x = new IterableString('This is a really cool test.');

while(x.hasNext()){system.debug(x.next());
}

 

(Note that there is no implementation provided for IterableString class)

 

 

global class CustomIterable implements Iterator<Account>{
List<Account> accs {get; set;}
Integer i {get; set;}
public CustomIterable(){
accs = [SELECT id, name, numberofEmployees FROM Account WHERE name = 'false'];
i = 0;
}
global boolean hasNext(){
if(i >= accs.size())
return false;
else
return true;
}
global Account next(){
if(i == 8){ i++; return null;}
i=i+1;
return accs[i-1];
}
}

 

global class foo implements iterable<Account>{
global Iterator<Account> Iterator(){
return new CustomIterable();
}
}

 

global class batchClass implements Database.batchable<Account>{
global Iterable<Account> start(Database.batchableContext info){
return new foo();
}
global void execute(Database.batchableContext info, List<Account> scope){
List<Account> accsToUpdate = new List<Account>();
for(Account a : scope){
a.name = 'true';
a.numberOfEmployees = 69;
accsToUpdate.add(a);
}
update accsToUpdate;
}
global void finish(Database.batchableContext info){
}
}

 

Code samples are a bit sloppy (i.e. "if(i == 8){ i++; return null}") and don't really demonstrate anything useful. 

 

Edit 12/17/2009: Revised some of my earlier comments - which were a bit nitpicky and not entirely correct. 

 

Message Edited by glorge on 12-17-2009 10:01 AM