8

According to the documentation, in a batch a Database.QueryLocator can retrieve up to 50 million records.

A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.

I then cycle through them in batches, default size 200, maximum size 2000. When I run a batch, Salesforce knows how many batches there are, so it must know how many records are in the QueryLocator. But can I find out? The only methods I can see on the QueryLocator are these two:

getQuery() Returns the query used to instantiate the Database.QueryLocator object. This is useful when testing the start method.

iterator() Returns a new instance of a query locator iterator.

I want to know what the total size of the record set is, before I go through all the batches.

  • Really looking forward to some insights on this! Salesforce use their super powers to recurse to the end of the data set with QueryLocators and Custom Iterators, which always surprised me as I figured that to be a one way function! Wonder if they are cloning the cursor under the covers and consuming that to accumulate the count. – bigassforce Dec 21 '14 at 16:13
  • 2
    According to andy fawcett, in batch the query start stores away the entire recordset which is then consumed as if from a cache by the execute method. So somewhere the platform has already completed the query. – Dominic Dec 21 '14 at 21:53
2

Try running a SELECT COUNT() FROM ... query in your start method before calling QueryLocator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.