Find the best tutors and institutes for Salesforce Developer

Find Best Salesforce Developer Training

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

What Is Batch Apex And Its Methods ?

05/08/2017 0 0

1. We can call the apex code by creating object for the class (or) if the variables or methods are static then we can call with class name.

  • Apex Code in the trigger will execute automatically for the DML operations.

  • If you want to execute apex code on a specific time then we should write batch class.

  • With Batch Apex we can process maximum of 50 million records.

  • Batch Apex is asynchronous (execute in future context).

  • While writing the batch class we should inherit Database.Batchable interface.

  • Database is a built in global class which contains inner global interface.

2. What are the Batch Apex methods?

Since we are inheriting Database.Batchable interface we should implement all the method prototypes declared in the interface.

 We should implement the following global methods:

  • Start: It will prepare the records to process and execute only one time.

  • Execute: It will take the records prepared in start method and split those records into batches and it will execute multiple times. For example if the start method is returning 1000 records then execute method executes 5 times if you don't mention the batch size (Default Batch Size is: 200). Maximum batch size is: 2000.

  • Finish: We can perform post commit logic like sending emails with the success or error information. It will execute only one time.

Batch Class Syntax:

//Database is Class provided by Salesforce.

//Batchable is an global interface which is inside of the Database class.

 //Batchable include 3 method prototypes: 1. Start 2. Execute 3. Finish

 //start and finish methods will execute only one time.

 //execute method executes multiple times.

 global class BatchUsage implements Database.Batchable , Database.Stateful {

  //at a time we can inherit multiple interfaces but we cannot inherit multiple classes.

  //By default batch class is stateless (variable info. store in one method cannot be remembered in other method),

  //to make it stateful we should inherit Database.Stateful interface.

  String name = '';

  global Database.queryLocator start(Database.BatchableContext bc) {

  //From asynchronous to asynchronous we cannot call

  //(from batch class we cannot call future mehthod vice versa.).

  name += 'start';

  system.debug('@@@Start Method: '+name);

   //Collects the records to process. It will forward these records to execute method.

   //If we use Iterable as return type for this start method it can hold

   //max. of 50k records only.

   //If we use Database.queryLocator as return type for this start method it can hold

   //max. of 50 million records.

   return Database.getQueryLocator('select id, name, Location__c from Department__c where Location__c = \'\'');


  global void execute(Database.BatchableContext bc, LIST sObjLst) {

   name += 'execute';

   system.debug('@@@Execute Method: '+name);

  //Split the records forwarded by start method into batches.

  //Default batch size is: 200.

  //Max. batch size is: 2000.

  List depUPdLst = new List();

  for(SObject sObj: sObjLst) {

  Department__c dept = (Department__c) sObj;//Type Casting.

  dept.Location__c = 'Bangalore';



   if(depUPdLst.size() > 0)

    update depUPdLst;


  global void finish(Database.BatchableContext bc) {

   name += 'finish';

   system.debug('@@@Finish Method: '+name);

   //Post Commit logic like sending emails for the success or failures of the batches.

   AsyncApexJob a = [select id, Status, NumberOfErrors, JobItemsProcessed,

   TotalJobItems, CreatedBy.Email from AsyncApexJob where id =: bc.getJobId()];


   Messaging.singleEmailMessage mail = new Messaging.singleEmailMessage();

   mail.setToAddresses(new String[]{a.CreatedBy.Email});

   mail.setSubject('Batch Class Result');

   mail.setHtmlBody('The batch Apex job processed ' + '' + a.TotalJobItems + '' +

   ' batches with '+ '' + a.NumberOfErrors + '' + ' failures.');

   //sendEmail methods

   Messaging.sendEmail(new Messaging.singleEmailMessage[]{mail});


   /*** Scheduling in minutes or hours ***/

   /*//Create object for schedulable class

   SchedulableUsage su = new SchedulableUsage();

   //Preparing chron_exp

   Datetime sysTime =;

   sysTime = sysTime.addminutes(6);

   String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +

   sysTime.hour() + ' ' + + ' ' + sysTime.month() + ' ? ' + sysTime.year();           

   System.schedule('Dep Update'+sysTime.getTime(),chron_exp, su);*/



0 Dislike
Follow 0

Please Enter a comment


Other Lessons for You

Starting Career as Salesforce Admin
Hello Everyone! This is just an introductory lesson for those who want their career as Salesforce Admin. Being a Salesforce Admin, you do not need any prior experience on CRM. What you all need is the...

Devika G. | 19/04/2018

0 0

Salesforce Certification Course
Basics of Cloud Computing: Overview of each of three building blocks in cloud applications SAAS PAAS IAAS Different Cloud Service providers Salesforce Architecture: Introduction...

Difference Between Data Loader And Import Wizard In Salesforce
Difference between Dataloader and Import Wizard in Salesforce: Dataloader Import Wizard It supports more than 50000 records. It supports only up to 50000. It supports...

Siva Sankar | 07/02/2018

0 0

Java To Salesforce Connection
Java to Salesforce Connection Step 1: Generate or Obtain the Web Service WSDL To generate the WSDL file for your organization: Log in to your Enterprise, Unlimited, or Developer Edition Salesforce...

Siva Sankar | 07/02/2018

1 0

Before Take Salesforce Training Need To Ask Below Things
In every platform technology will be follow below things: i. Software Development Life Cycle--------SDLC having diffrent METHADOLOGIES (Salesforce SDLC -- Agile and Agile with SCRUM) What is the difference...

Siva Sankar | 07/02/2018

1 0

Find Best Salesforce Developer Training?

Find Now » is India's largest network of most trusted tutors and institutes. Over 25 lakh students rely on, to fulfill their learning requirements across 1,000+ categories. Using, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 6.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more