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
nikkeynikkey 

When do we use Iterable in Batch Apex?

Hi Folks , 

Can any one help out .I shall appreciate your help.

What is the use of query locator and iterable in batch apex and when do we use them?


Thanks in ADVANCE.



Anilkumar KotaAnilkumar Kota
Hi Nikkey,

QueryLocator is used when the scope is directly taken from a SOQL : 
 
Use the Database.QueryLocator object when you are using a simple query (SELECT) to generate the scope of objects used in the batch job. If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an organization. Another example is a sharing recalculation for the Contact object that returns a QueryLocator for all contact records in an organization.
 
Iterable is used when you need to create a complex scope for the batch job. You can also use the iterable to create your own custom process for iterating through the list.
Ex : example only showed a custom Iterator class, not the Batch Apex Class that would leverage such a class. You are correct in assuming that you would need two classes: your custom Iteration class plus a batch class that uses that class.