+ Start a Discussion
vijay bhaskarvijay bhaskar 

please tel me batch apex uses and how to exucute batch apex methods in salesforce

William TranWilliam Tran
Using Batch Apex

To use batch Apex, write an Apex class that implements the Salesforce-provided interface Database.Batchable, and then invoke the class programmatically.

To monitor or stop the execution of the batch Apex job, from Setup, click Monitoring | Apex Jobs or Jobs | Apex Jobs.Implementing the Database.Batchable InterfaceThe Database.Batchable interface contains three methods that must be implemented.start method:
view source

1 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 pass to the interface method execute. This method returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed into the job.

Use the Database.QueryLocator object when you are using a simple query (SELECT) to generate the scope of objects used in the batch job. If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an organization. Another example is a sharing recalculation for the Contact object that returns a QueryLocator for all account records in an organization.

Use the iterable to create a complex scope for the batch job. You can also use the iterable to create your own custom process for iterating through the list.


If you use an iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.

execute method:
view source

1 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:

A reference to the Database.BatchableContext object.
A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, use the returned list.

Batches of records tend to execute in the order in which they’re received from the start method. However, the order in which batches of records execute depends on various factors. The order of execution isn’t guaranteed.

finish method:
view source

1 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.

Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each. The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.

Follow this link for more info:

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.