Activity Forums Salesforce® Discussions How can we schedule a Batch Apex Class in Salesforce?

  • How can we schedule a Batch Apex Class in Salesforce?

  • sushant

    Member
    December 6, 2016 at 1:17 pm

    Hi All

    How can we schedule a Batch Apex Class?

    Please give suggestions

    Thanks

    Salesforce

  • Satyakam

    Member
    December 14, 2016 at 6:53 am

    Hi Sushant,

    We use scheduler class to schedule batch class.

    Example of a batch class:
    global class BatchClass 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){
    }

    }

    Example of a Scheduled Apex:

    global class scheduledBatchable implements Schedulable{
    global void execute(SchedulableContext sc) {
    // Implement any logic to be scheduled

    // We now call the batch class to be scheduled
    ExampleBatchClass b = new ExampleBatchClass();

    //Parameters of ExecuteBatch(context,BatchSize)
    database.executebatch(b,10);
    }
    }

    Schedule the class by executing anonymous

    Finally now we can schedule the batch class, there are two ways by which we can schedule the batch class
    1.From Setup—> Apex Classes –> Schedule Apex : but, here the minimum is one day/ 24 hours
    By executing anonymous code from either developer console or apex, here the minimum is 1 hour

    Code to be executed

    // Cron EXP for hourly schedule
    String CRON_EXP = '0 0 * * * ?';
    SheduledBatchable sch = new scheduledBatchable();
    system.schedule('Hourly Example Batch Schedule job', CRON_EXP, sch);

    this may help you.

    Thanks

  • William

    Member
    July 26, 2018 at 3:57 am

    To write a Batch Apex class, your class must implement the Database.Batchable interface

    An interface is like a class in which none of the methods have been implemented—the method signatures are there, but the body of each method is empty.

    To use an interface, another class must implement it by providing a body for all of the methods contained in the interface.

    Implementing the Database.Batchable Interface

    The Database.Batchable interface contains three methods that must be implemented.

    start()
    global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}

    Note: If your code accesses external objects and is used in batch Apex, use Iterable<sObject> instead of Database.QueryLocator.

    execute()
    global void execute(Database.BatchableContext bC, list<P>){}
    finish()
    global void finish(Database.BatchableContext bC){}

    Sample Code

    global class ExampleBatchClass implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
    return Database.getQueryLocator(‘select name, NumberOfEmployees from account’);
    }

    global void execute(Database.BatchableContext info, List<Account> scope){
    List<Account> accsToUpdate = new List<Account>();
    for(Account a : scope){
    a.Name = 'true';
    a.NumberOfEmployees = 70;
    accsToUpdate.add(a);
    }
    update accsToUpdate;
    }
    global void finish(Database.BatchableContext info){
    }
    }

Viewing 1 - 3 of 3 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos