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
SKT CloudComputingSKT CloudComputing 

Batch class in Apex??

I have below questions w.r.t. batch class.

1- What is batch apex class?

2- what are all  the methods defined in batch apex class?

3- what are the return types and syntaxes  of all methods of batch apex class?

4- when we need to go for batch apex class?


It would be a great help if anyone can shar one's own experience for these questions rather than sharing link of documents.

Thanks!
Rupali PophaliyaRupali Pophaliya
1- What is batch apex class?
>> It allows you to define a job that can be divided into manageable chunks, where each chunk can be proceed separately.

2- what are all  the methods defined in batch apex class?
>> To use Batch Apex, you must implement “Database.Batchable”. This interface has three methods. those are:
1. Start
2. execute
3. Finish

3- what are the return types and syntaxes of all methods of batch apex class?

Start:
global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}
Execute: 
global void execute(Database.BatchableContext BC, list<P>){}

Finish:
global void finish(Database.BatchableContext BC){}

4- when we need to go for batch apex class?
>> when you want to do a bulk job.
    To have higher governor limits. 
    We can schedule to run at different time.
SKT CloudComputingSKT CloudComputing
Thanks Ruplai for nice explanation.

Could you please brief me about return type of these methods,

Thanks!
Rupali PophaliyaRupali Pophaliya
Start method returns either a Database.QueryLocator object or an iterable that contains the records or objects passed to the job.

Use the Database.QueryLocator object when you are using a simple query to generate the scope of objects used in the batch job. In this case, the SOQL data row limit will be bypassed.
Use iterable object when you have complex criteria to process the records. And, governor limit for the total number of records retrieved by SOQL queries is still enforced, if you use this.

Execute method gets called after the Start method and does all the processing required for Batch Job. list<P> parameter from given syntax is returned by the Database.QueryLocator method. Return type of this method is void.

Finish method executes after all batches are processed. You can use this method to send confirmation email notifications or execute post-processing operations. Return type of this method is void.