Sunday 29 May 2016


In order for us to write batch classes in apex, let us 1st understand what is batch class , when and where it should be used? This is an extensive blog going into details.
Main sections of this post are
  1. What is batch Apex
  2. When to use batch apex
  3. Advantages of using batch apex
  4. Batch Apex Governor limits
  5. Sample Code

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.  

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. To  learn more about the governor limits go here

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 e.g. Send More than 10 E-mails blog by Ankit Arora

  • here is the governor limit difference in using batch
    Normal Context
    Batch Context
    SOQL queries100 SOQL per cycle200 SOQL per cycle
    records retrieved by SOQL queries50,00050,000,000 (getQueryLocator)
    executed code statements200,0001,000,000 (Winter '14)
    Heap size6 MB12 MB

    Batch Apex Governor Limits

    These are the governor Limits you need to keep in mind when dealing with Batch Apex
    • Up to five queued or active batch jobs are allowed for Apex.
    • A user can have up to 50 query cursors open at a time. For example, if 50 cursors are open and a client application still logged in as the same user attempts to open a new one, the oldest of the 50 cursors is released. Note that this limit is different for the batch Apex startmethod, which can have up to five query cursors open at a time per user. The other batch Apex methods have the higher limit of 50 cursors. Cursor limits for different features are tracked separately. For example, you can have 50 Apex query cursors, 50 batch cursors, and 50 Visualforce cursors open at the same time.
    • A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
    • If the start method returns a QueryLocator, the optional scope parameter ofDatabase.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you may run into other limits.
    • If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.
    • The startexecute, and finish methods can implement up to 10 callouts each.
    • Batch executions are limited to 10 callouts per method execution.
    • The maximum number of batch executions is 250,000 per 24 hours.
    • Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn't cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running.

    Sample Code

    Batch Class :
       global class ExampleBatchClass implements Database.Batchable<sObject>{
            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, List<sObject>scope){
                  // 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)
    Note : if batch size is not mentioned it is 200 by default. 

    For More About Batch Job: For More Info's:


    1. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
      Digital Marketing online training

      full stack developer training in pune

      full stack developer training in annanagar

    2. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!

      click here

      Selenium Training in Bangalore|
      Selenium Training in Chennai

    3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
      python training institute in chennai
      python training in Bangalore
      python training in pune
      python training institute in chennai
      python training in velachery
      python online training

    4. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.

      Blueprism training in btm

      Blueprism online training

    5. This is ansuperior writing service point that doesn't always sink in within the context of the classroom. In the first superior writing service paragraph you either hook the reader's interest or lose it. Of course your teacher, who's getting paid to teach you how to write an good essay, 
      java training in chennai | java training in bangalore

      java training in tambaram | java training in velachery

    6. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.
      angularjs-Training in tambaram

      angularjs-Training in sholinganallur

      angularjs-Training in velachery

      angularjs Training in bangalore

      angularjs Training in bangalore

    7. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
      devops online training

      aws online training

      data science with python online training

      data science online training

      rpa online training

    8. Great post. keep sharing such a worthy information.
      AWS course in Chennai