What is Batch Class in Apex?

Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks. We could build a data cleansing operation that goes through all Accounts and Opportunities on a nightly basis and updates them if necessary, based on custom criteria.

Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be programmatically invoked at runtime using Apex.

Need of Batch Apex:

As we know about the salesforce governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.

But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/editing/deleting them when needed.

Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.

We have to create a global apex class that extends Database. Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs. Below is a sample class that is designed to delete all the records of Account object (Let’s say your organization contains more than 50 thousand records and you want to mass delete all of them).

Methods:

Database.Batchable interface contains 3 methods that must be implemented:

start() :

It collects the records or objects to pass to the interface method execute(), call the start() at the beginning of a BatchApexJob. This method returns either a Database.QueryLocator object that contains the records passed to the job.

execute() :

To do the required processing of each chunk of data, use the execute method. This method is called for each batch of records that you pass to it. This method takes a reference to the Database.BatchableContext object.

finish() :

To send confirmation emails or execute post-processing operations, we use finish(). This method is called after all batches are processed.

Note: The order of execution of batches is not guaranteed.

Example:

global class deleteAccounts implements Database.Batchable

{

global final String Query;

global deleteAccounts(String q)

{

Query=q;

}

global Database.QueryLocator start(Database.BatchableContext BC)

{

return Database.getQueryLocator(query);

}

global void execute(Database.BatchableContext BC, List scope)

{

List <Account> lstAccount = new list<Account>();

for(Sobject s : scope)

{

Account a = (Account)s;

lstAccount.add(a);

}

Delete lstAccount;

}

// Send an email to the User after your batch completes

global void finish(Database.BatchableContext BC)

{

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

String[] toAddresses = new String[] {‘sforce2009@gmail.com’};

mail.setToAddresses(toAddresses);

mail.setSubject(‘Apex Batch Job is done‘);

mail.setPlainTextBody(‘The batch Apex job processed ‘);

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

}

}

// This is how the batch class is called.

id batchinstanceid = database.executeBatch(new deleteAccounts(‘select Id from Account’));

Our Recent Blog

Share This Post