Batch Apex

Batch Apex in Salesforce (Basics, Governor Limits, Custom Iterable of Batch)

Transaction limits in APEX

  • Absolute number of SOQL queries issued 1 - 100 
  • Absolute number of records recovered by SOQL queries- 50,000 
  • Absolute number of records recovered by Database.getQueryLocator - 10,000 
  • Absolute number of records recovered by a SOSL inquiry - 2,000 
  • Absolute number of DML issued 2 - 150 
  • Absolute number of records handled because of DML proclamations, Approval process, or database.emptyRecycleBin - 10000 
  • Complete number of callouts (HTTP demands or Web administrations calls) in an exchange - 100 
  • Most extreme aggregate break for all callouts (HTTP demands or Web administrations calls) in an exchange - 120 
  • The most extreme number of techniques with the future explanation permitted per Apex summon - 50 
  • Greatest number of Apex occupations added to the line with System.enqueueJob - 50 
  • Absolute number of sendEmail strategies permitted - 10 
  • Most extreme execution time for every Apex exchange - 10 least 
  • Most extreme number of pop-up message strategy calls permitted per Apex exchange - 10 
  • Most extreme number of message pop-ups that can be sent in each message pop-up technique call - 2000

Database.Batchable Interface

To utilize batch Apex, compose an Apex class that executes the Salesforce-gave interface Database.Batchable and afterward summon the class automatically. 

To stop the execution of the Batch Apex work, Go to Setup and enter Apex Jobs in the Quick Find box, and select Apex Jobs. 

dont miss out iconDon't forget to check out: Concept of Virtual and Abstract Modifiers in Apex | Salesforce Developer Guide

Method in Batchable Interface

  • Start() 
  • Execute() 
  • Finish() 

Batch Apex in Salesforce is uncommonly intended for an enormous number of records, i.e., here, information would be isolated into little clumps of records, and afterward, it would be assessed. All in all, the Batch class in Salesforce is explicitly intended to handle mass records together and have a more prominent lead representative breaking point than the simultaneous code. 

Start: This strategy is called at the beginning of a batch task to gather the information on which the Batch job will be working. It breaks the information or record into batches. Now and again, the 'QueryLocator' technique is utilized to work with the basic SOQL query to generate the scope of objects inside a batch job. 

Asynchronous operations can be carried out by Batch Apex classes

Batch occupations are invoked programmatically during the runtime and can be worked on any size of records, with a limit of 200 records for each clump. You can separate bigger record information into 200 records for each clump to execute it better.


With the QueryLocator object, the lead agent cutoff for unquestionably the quantity of records recuperated by SOQL requests is evaded and you can address up to 50 million records.

In any case, with an Iterable, the lead agent limit for without a doubt the quantity of records recuperated by SOQL requests is at this point approved. 

QueryLocator object or an Iterable that contains the records or items passed to the work.

Custom Iterable

The iterator technique should be proclaimed as worldwide or public.

In the accompanying model a custom iterator emphasizes through an assortment:

global class Iterables
implements Iterator<Contact>{
    List<Contact> con{get; set;}
    Integer i {get; set;}
    public Iterables(){
        [SELECT Id, Name,
        FROM Contact
        WHERE Name = 'false'];
        i = 0;
    global boolean hasNext(){
        if(i >= con.size()) {
            return false;
        } else {
            return true;
    global Contact next(){
        // 8 is an arbitrary
        // constant in this example
        // that represents the
        // maximum size of the list.
        if(i == 8){return null;}
        return con[i-1];
global class example implements iterable<contact>{
    global Iterator<Contact> Iterator(){
        return new Iterables();
global class batchClass implements Database.batchable<Contact>{
    global Iterable<Contact> start(Database.batchableContext info){
        return new example();
    global void execute(Database.batchableContext info, List<Contact> scope){
        List<Contact> conToUpdate = new List<Contact>();
        for(Contact a : scope){
            a.Name = 'true';
            a.NumberOfEmployees = 69;
        update conToUpdate;
    global void finish(Database.batchableContext info){}

dont miss out iconCheck out another amazing blog by Ratnesh here: Preparation for Unplanned Downtime in Salesforce

Execute Method

Used to gather the records of objects to be passed to the interface methods executed for preparing. 

This technique is called once toward the start of a Batch Apex work and returns either a Database.QueryLocator object or an Iterable that contains the records or articles passed to the job.


Popular Salesforce Blogs