UrbanPro
true

Learn Salesforce Developer from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

What Is Batch Apex And Its Methods ?

Kumar
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';

  depUPdLst.add(dept);

   }

   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 = System.now();

   sysTime = sysTime.addminutes(6);

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

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

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

  }

 }

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

SFDC ADMIN
Everything is Configuration. Logical Data Diagramatic Approach Business Analysis Required Implementation the Architecture Internal Stack holders Discussions.

Importance of Pagelayout in salesforce
𝐖𝐡𝐚𝐭 𝐚𝐫𝐞 𝐟𝐞𝐚𝐭𝐮𝐫𝐞𝐬 𝐭𝐡𝐚𝐭 𝐜𝐚𝐧 𝐛𝐞 𝐜𝐨𝐧𝐭𝐫𝐨𝐥𝐥𝐞𝐝 𝐮𝐬𝐢𝐧𝐠 𝐩𝐚𝐠𝐞 𝐋𝐚𝐲𝐨𝐮𝐭?>>Control which fields, lists of related records, and custom links userscan see>>Customize...

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...

Trigger Best Practices
Triggers - Best Practices in Salesforce 1. Future methods, SOQL and DML: Avoid writing Future methods, SOQL and DML inside the "For" loop.2. Bulkify the trigger: Start developing the logic...
R

Rajiv Kumar Singh

0 1
0

What is SFDC Architecture.
SFDC is built based on the MVC Pattern.Model - Which is handle the Database Objects View - Which will take care of the UI pages. Controllers- The business logic Implementation will be done here.
X

Looking for Salesforce Developer Classes?

The best tutors for Salesforce Developer Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Salesforce Developer with the Best Tutors

The best Tutors for Salesforce Developer Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. 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