Batch Apex in Salesforce

What is Batch Apex ?
Batch as the name suggests, is used when a large data (bulk) volume is involved and it has to be redundantly processed using a particular logic.
The Batch apex, can be used to conveniently perform time to time task and some real complex job ranging from data cleansing, archiving the data to the other quality improvements.

To monitor or stop the execution of the batch Apex job, go to Setup –> Monitoring –> Apex Jobs. For more information, see “Monitoring the Apex Job Queue” in the Salesforce online help.

When to use Batch Apex?
The typical use of batch apex is when you want to do a bulk job, but if you do it in a regular apex you’re bound to hit the governor limits. Batch Classes process the set of records you pass to it in batches of maximum 200 per batch.

Advantages of using Batch Apex?

Higher Governor Limits
Can be used to process in batches
Can be scheduled to run at different time. (read more)
Work around to other governor limits

Batch Class :

global class ExampleBatchClass implements Database.Batchable{

global ExampleBatchClass(){
// Batch Constructor
}

// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}

// Execute Logic
global void execute(Database.BatchableContext BC, Listscope){
// Logic to be Executed batch wise

}

global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}

Call the Batch Class :

ExampleBatchClass b = new ExampleBatchClass();
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,10);

Note : if batch size is not mentioned it is 200 by default.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *