Batch Class and Scheduler Apex Salesforce – coderinme

If you want to send email to your sales team every morning, or do you want to check the attedance of all your employee in company, how apex will help in salesforce.we will need something.

Batch Class and Scheduler  Apex

Scheduler is used to schedule a class or bunch of task at the specific time for once or at regular interval.  Batch class is the class that will be scheduled, it is a special type of class for bulk records. it will help us to overcome

  • cpu time limit
  • heap size issue.
  • governer limit
  • schedule at a particular time
  • where salesforce workflow or process builder fails

want to learn more about salesforce coding ?

So first of all please take a look of  sample code of batch class.

global class batchAccountUpdate implements Database.Batchable<sObject> {
 
    //start
    global Database.QueryLocator start(Database.BatchableContext BC){
       // query from an object for records 
    }
    //execute
    global void execute(Database.BatchableContext BC,List<Account > Scope){

        // here we will write logic for that what we want to do
    }
    //finish
    global void finish(Database.BatchableContext BC){

       /* after the completion of batch anything we want to do like a confirmation of               batch execution/*             
    }

}

Now we will see how scheduler class will look like for above batch class

global class scheduledBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
      // batch class name do you want to call
      batchAccountUpdate b = new batchAccountUpdate(); 
      // size of batch how much record in single batch
      database.executebatch(b,200);
   }
}

How we will schedule it:

Go to Apex class , and find the following and click on it.Batch Class and Scheduler Apex

choose the scheduler class and schedule it accordingly. and save it. voila.

Batch Class and Scheduler

if you wan to run a batch only once than go to developer console and open anonymous window

and write the batch class name like that.

batchAccountUpdate b = new batchAccountUpdate(); 
      // size of batch how much record in single batch
database.executebatch(b,200);
Let’s see these code what it will do.

it will take all account and it will rename name of account with name + number like i have 5 account with name
saif, luqs, prabhu, sam, harry.
when the batch class will execute it will update it as
saif1, luqs2, prabu3, sam4, harry5

global class SampleBatch implements Database.Batchable<sObject> {

  
  // The batch job starts
  global Database.Querylocator start(Database.BatchableContext bc){
    
    //string query='SELECT Id, Name FROM Account';
    return null;
    
  } 
  
  // The batch job executes and operates on one batch of records
  global void execute(Database.BatchableContext bc, List<sObject> scope){

    list<account> ac = [SELECT Id, Name FROM Account];
    integer i=1;
    for(Account a: ac){
        a.name=a.name+i;
        i++;
    }
    update ac;
    system.debug('mine'+ac);
  }
  
  // The batch job finishes
  global void finish(Database.BatchableContext bc){
    AsyncApexJob job = [SELECT Id, Status FROM AsyncApexJob WHERE Id = :bc.getJobId()]; 
    System.debug('>>>> finish ' + job.Status);
  }
}

we will discuss it soon, till then learn code and enjoy.

Video tutorial for this article is also available

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.