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
Satish PalyamSatish Palyam 

Batch Apex for Large Volume, start query

Hi Salesforce Developers Community,

We have a need for batch apex where we have around 6M accounts to be updated with right owner, based on custom territory ruels. 

Start Query: As i need to first fetch all the accounts i need to have query where Select field1,field2, field3, field4 from Account which wil return Database.QueryLocator. Since the voulme is so high, can i still go ahead with this logic ? what happens when the Account records are updated in execute method, will this impact Database.QueryLocator to pick up the same records again ?

 
Nayana KNayana K
In start() , Database.QueryLocator takes up to 50M records.

When you specify scope while executing batch, records will split into chunks of scope size.
Eg :
Database.executeBatch(new BATCHCLASSNAME(), 5);

Here records will split into chucks of size 5. Execute() method will take 5 records at a time and logic inside execute() will be performed.

The value for scope must be greater than 0.
The scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 200 records. However, if you use a very high number, you may run into other limits.

You can go ahead with batch.


 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for batch job related query
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html

Batch Apex
A Batch class allows you to define a single job that can be broken up into manageable chunks that will be processed separately.

When to use Batch Apex
One example is if you need to make a field update to every Account in your organization. If you have 10,001 Account records in your org, this is impossible without some way of breaking it up. So in the start() method, you define the query you're going to use in this batch context: 'select Id from Account'. Then the execute() method runs, but only receives a relatively short list of records (default 200). Within the execute(), everything runs in its own transactional context, which means almost all of the governor limits only apply to that block. Thus each time execute() is run, you are allowed 150 queries and 50,000 DML rows and so on. When that execute() is complete, a new one is instantiated with the next group of 200 Accounts, with a brand new set of governor limits. Finally the finish() method wraps up any loose ends as necessary, like sending a status email.


Sample Batch Apex
1) Start method
 is automatically called at the beginning of the apex job. This method will collect record or objects on which the operation should be performed. These record are divided into subtasks & passes those to execute method.

2) Execute Method performs operation which we want to perform on the records fetched from start method.

3) Finish method executes after all batches are processed. Use this method to send confirmation email notifications.

Let us know if this will help you