Batch apex in Salesforce

Batchable Interface

Database.Batchable is an interface in salesforce which has following methods

  1. global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc);
  2. global void execute(List<Database.BatchableContext bc, sObject>);
  3. global void finish(Database.BatchableContext bc);
global class BatchExample implements Database.Batchable<sObject> {
    public BatchExample() {

}

global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator(‘select id, name, Ownerid from Account’);
}

global void execute(Database.BatchableContext bc, List<Account> acc){
for(Account a : acc){
a.name = a.name + ‘change’;
}

update acc;
}

global void finish(Database.BatchableContext bc){
system.debug(‘done’);
}

}

How to run the Batch

  1. Direct Calling of Database.executeBatch(BatchableClass, scope)

Database.executeBatch() take an optional parameter scope which specifies the number of records to pass into the execute method. This will limits number of record pass for each transaction.

default value for scope is 200

This method return id of AsyncApexJob object, using this id you can track progress;

When you call Database.executeBatch, Salesforce only places the job in the queue. Actual execution can be delayed based on service availability.

BatchExample be = new BatchExample();
ID batchprocessid = Database.executeBatch(be);

AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors
FROM AsyncApexJob WHERE ID =: batchprocessid ];

  1. Calling via Database.scheduleBatch(BatchableClass, ‘Job Name’, timeIntervalMinute, scope)

‘job Name’ is a string parameter

timeIntervalMinute time after which the job will start running

optional scope variable describe above

String cronID = System.scheduleBatch(be, 'job example', 1);

CronTrigger ct = [SELECT Id, TimesTriggered, NextFireTime
FROM CronTrigger WHERE Id = :cronID];

// TimesTriggered should be 0 because the job hasn’t started yet.
System.assertEquals(0, ct.TimesTriggered);
System.debug(‘Next fire time: ‘ + ct.NextFireTime);

Monitor

  1. To monitor or stop the execution of the batch Apex job, from Setup, enterApex Jobs in the Quick Find box, then select Apex Jobs.
  2. If you started batch with Database.executeBatch(job) use the id returned by the method and table AsyncApexJob
  3. If you started with System.scheduleBatch use the id returned by this method and table CrontTrigger.

Governor Limits

  • Up to 5 batch jobs can be queued or active concurrently.
  • Up to 100 Holding batch jobs can be held in the Apex flex queue.
  • In a running test, you can submit a maximum of 5 batch jobs.
  • The maximum number of batch Apex method executions per 24-hour period is 250,000, or the number of user licenses in your org multiplied by 200—whichever is greater. Method executions include executions of the start, execute, and finish methods. This limit is for your entire org and is shared with all asynchronous Apex: Batch Apex, Queueable Apex, scheduled Apex, and future methods.

Leave a Reply

Scroll to top