How to write batch apex class in Salesforce Apex | Use batch processing in Salesforce Apex

by - 12:55 PM

To use Batch apex, you have to implement 'Batchable' Interface in your apex class. When you implement that interface, you have to write definition in these methods.

  • start          (grab the record you want to operate upon)
  • execute     (perform the operation)
  • finish       (close, and if required send confirmation email)


Real World Example : 

Add a keyword 'updated' in the name of all opportunities and then roll it back. To test it, we will run a simple salesforce report to see effect.

Call method 'executeBatch' to start batch processing

Run a Salesforce Report

Create a report to see, if  the changes are visible on opportunity names

Look good, lets roll it back

Send Email on success

Simply add code to send email in the 'finish()' method see below

When to use Batch Process ?

Batch job are made to perform common UPSERT operation on a scheduled basis. 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.

Read more example and best practices here

You May Also Like