Iterable Interface in salesforce

Iterable Interface in salesforce

Using Custom Iterators with Iterable

If you do not want to use a custom iterator with a list, but instead want to create your own data structure, you can use the Iterable interface to generate the data structure.

The Iterable interface has the following method:

Name Arguments Returns Description
iterator Iterator class Returns a reference to the iterator for this interface.

The following calls the Custom Iterator code (click here to see the code which it calls ):

global class IterableExample implements iterable<Contact>
global Iterator<Contact> Iterator()
return new CustomIterable(); 

Consideration of Iterable Interface in Batch Apex :-

  • If you use an iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.
  • If your code accesses external objects and is used in batch Apex, use Iterable<sObject> instead of Database.QueryLocator.

Be the first to comment

Leave a Reply

Your email address will not be published.