Salesforce Asked on December 31, 2021
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.
As per the documentation, we do not have any existing functions to get the size.
You can use the following code to get the total size of returned values:-
global class YourBatchClass implements Database.Batchable<sObject>, Database.Stateful{
global Integer countVariable=0;
global Database.QueryLocator start( Database.BatchableContext BC ) {
String queryStr = 'select id,name from Contact';
Database.QueryLocator queryResult= Database.getQueryLocator( queryStr );
// Get an iterator
Database.QueryLocatorIterator i = queryResult.iterator();
// Iterate over the records
while (i.hasNext())
{
countVariable++;
}
system.debug('Total returned Records: '+countVariable);
return queryResult;
}
Answered by Vermajai1995 on December 31, 2021
Try running a SELECT COUNT() FROM ...
query in your start method before calling QueryLocator.
Answered by Scott VonSchilling on December 31, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP