+ Start a Discussion
ANKITAANKITA 

Difference between Database.QueryLocator() and Iterable in BatchApex?

Can u please tell me the diff btn Iterable and Database.Querylocator() in Batch apex?

Just tell me few words.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Ankita,

 

It depends on your need , if you want to run batch on records that can be filtered by SOQL then QueryLocator is preferable, but if records that you want to bee processed by batch can not be filtered by SOQL then you will have to use iteratable. But most of the cases it will be achieved  by query locator , so query locator is preferable so just try with it if you scope is complex and can not be achieved by SOQL then go with iterable.

All Answers

Shashikant SharmaShashikant Sharma

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. The idea here is that the Force.com platform basically does this:

 

public class Database {
  public static void executeBatch(Database.Batchable batch) {
    executeBatch(batch, 200);
  }
  public static void executeBatch(Database.Batchable batch, Integer limit) {
    Database.BatchableContext BC = new Database.BatchableContext();
    Database.Iterator iterator = batch.start(BC);
    List<Object> items = new List<Object>();
    while(iterator.hasNext()) {
      items.add(iterator.next());
      if(items.size()==limit) {
        batch.execute(BC,items);
        items.clear();
      }
    }
    batch.finish(BC);
  }
}

 

Of course, the logic that the Force.com platform uses has greater complexity, including resetting governor limits, etc, but this is the basic design of the Batchable/Iterator interface. As long as your iterator returns the correct values for hasNext and next, it can iterate through any sort of set of data you could imagine, including mathematical sequences, extended search results from a service such as Twitter, and other large data sources.

 

 

ANKITAANKITA

Hi Shashi  thanks for your reply.

 

Between Database.QueryLocator() and Iterable which method is always preferable?

 

 

Shashikant SharmaShashikant Sharma

Ankita,

 

It depends on your need , if you want to run batch on records that can be filtered by SOQL then QueryLocator is preferable, but if records that you want to bee processed by batch can not be filtered by SOQL then you will have to use iteratable. But most of the cases it will be achieved  by query locator , so query locator is preferable so just try with it if you scope is complex and can not be achieved by SOQL then go with iterable.

This was selected as the best answer
ANKITAANKITA

Hi Shashi

You are doing great job for Community.

Expecting the same result in future.

Thanku so much.

 

Regards:

ANKITA

 

 

 

 

heamanheaman

I  depends  Database  query  is  not  recommend  incase   huge   query results  for  instance  search  result  for  lage   filter  combinations

crop1645crop1645

I used a custom iterator when processing a CSV file using batch APEX.  SOQL is obviously not available and since the CSV file could be large, batch apex was the way to go to avoid governor limits. The iterator reads each CSV line from a string using a csv parser. The iterator's next() method returns an array of strings, representing the column values for one CSV row

 

You could also use this approach for processing a large XML input that would otherwise blow up your heap or script statement limit

sebastiangnagnarellasebastiangnagnarella

You want to use an iterator also when you need to use Agregations in your Queries.

Something important is that when doing that your Iterator is actually processing a query and you are conditioned by the 50K records governor limit.