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
Ashritha ReddyAshritha Reddy 

hii can any one find the solution for below scenario?

Scenarios to use batch apex and best practices in using batch apex?
-------->I have 10000 records in account /any other object how can i split them into batches in batch apex ,and how many batches is possible to do like that.. in that total how many times should batch apex methods invoked(i mean start, execute and stop methds)?
Mahesh DMahesh D
Hi Ashritha,

Below information is for your questions:

As part of Batch Apex, by defailt it will take 200 records to process and it will divide the results of initial query which provide to process as part of the Batch class.

In your case 10000 records

It will divide into 50 batches and execute each batch.

Here Start method will execute only once.

Ececute method will execute based on number of baches and here in this example its 50.

Finish method will execute only once at the end to send an email to administrator about the results of Batch Apex.

Please check below post for Batch job:


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.

Please go through the below links:

Please do let me know if it helps you.

JyothsnaJyothsna (Salesforce Developers) 
Hi Ashiritha,

The minimum size for Batch Apex in Salesforce is 1.

The maximum size for Batch Apex in Salesforce is 2000.

The default size for Batch Apex is Salesforce is 200.

If you have 10,000 records, then you can split into 5 batches, and batch size is 2000.

Start and finish methods are execute only once. Execute method is execute based on records and batch size. In the above scenario, each batch contains 2000 records and execute 5 times.

start method

global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}
The start method is called at the beginning of a batch Apex job. Use the start method to collect the records or objects to be passed to the interface method execute. This method returns either a Database.QueryLocator object or an iterable that contains the records or objects being passed into the job.

execute method

global void execute(Database.BatchableContext BC, list<P>){}
The execute method is called for each batch of records passed to the method. Use this method to do all required processing for each chunk of data.

This method takes the following:

1. A reference to the Database.BatchableContext object.

2. A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, the returned list should be used.

Batches of records are not guaranteed to execute in the order they are received from the start method.

finish method

global void finish(Database.BatchableContext BC){}

The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations.

Please refer the below link for more details.

​Hope this helps you!
Best Regards,
Balakrishna N 25Balakrishna N 25

Nice explanation . i want to know the importance of SCOPE in Execute method, is it possible to randomly jobs can run in Batch apex like ( now i given 2000, I wantted to run 1500 then 400 ....Like) please give any suggestions
Mahesh DMahesh D
You can do it by passing a parameter to Batch Apex Constrcutor.

Like Below:

BatchClass b = new BatchClass(id,nlimit);

In the Batch Apex, get the limit value and add it into the query before it process in the Start method.

Constructor looks like below:

global BatchClass(String id, Integer qlimit) {
        Query = 'select id,  from Cust_Object__c where Status = \'ABC\' ';
        if (id != null && id != '') 
            Query = Query + ' and (id in (\''+id+'\'))';
        if (qlimit != null && qlimit >= 0) 
            Query = Query +' LIMIT '+qlimit;

Please do let me know if it helps you.

Ravi Dutt SharmaRavi Dutt Sharma
Hey Ashrita,

You can specify the batch size while scheduling the batch job. By default, the batch size is 200. So if you have 10000 records, there would be 10000/200 = 50 batches. The start method would be called once, in which the 10000 records would be queried. Now if you have specified the batch size as 200, then 200 records would be passed to execute method and the execute method would be called 50 times. After thr 50th execution has completed, the finish method would be called. So in short, start method would be called once, execute method would be called 50 times and finish method would be called once.
Mahesh DMahesh D
Hi Ashritha,

Once you satisfied the responses, please do validate and mark it as solved so that it will be helpful to others.

sreenivas Giddamsreenivas Giddam
Hello Ashritha Reddy,
1. Batch class run in at a sigle time.Batch apex methods start and finish methods also 1 time will excecute, But Execute () will run based on batch size..
2.if you having 10000 records.then you have to  run i.e batch process = 10000/batch size .Here by default batch size= 200,Max Batch size=2000, Min batch Size=200