What Is Batch Apex?

In the world of Salesforce, Batch Apex is your data powerhouse. It processes large amounts of data in manageable chunks, keeping governor limits in check and your system running smoothly. Batch Apex breaks down your massive data jobs into smaller, more digestible pieces – hence the name ‘Batch.’ Batch Apex’s 3-Step Dance Batch Apex is […]

0

In the world of Salesforce, Batch Apex is your data powerhouse. It processes large amounts of data in manageable chunks, keeping governor limits in check and your system running smoothly. Batch Apex breaks down your massive data jobs into smaller, more digestible pieces – hence the name ‘Batch.’

Batch Apex’s 3-Step Framework

Batch Apex is built around three main methods:

  1. start()

  2. execute()

  3. finish()

Let’s dive into the coding sea to understand what these methods do.

The Framework of Batch Apex

A Batch Apex class implements the Salesforce-provided Database.Batchable<sObject> interface, and then defines its methods. Sounds complex? Don’t worry; let’s break it down with a simple example:

global class BatchClassExample implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id FROM Account');
    }
    
    global void execute(Database.BatchableContext bc, List<Account> records) {
        for(Account record : records) {
            record.Name = 'Updated Account';
        }
        update records;
    }
    
    global void finish(Database.BatchableContext bc) {
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
        TotalJobItems, CreatedBy.Email
        FROM AsyncApexJob WHERE Id = :bc.getJobId()];
        
        // Send an email to the Apex job's submitter notifying job completion.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {a.CreatedBy.Email});
        mail.setSubject('Apex Sharing Recalculation ' + a.Status);
        mail.setPlainTextBody
        ('The batch Apex job processed ' + a.TotalJobItems +
        ' batches with '+ a.NumberOfErrors + ' failures.');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

start(): In our example, start() is selecting all the account records to process. We’re getting these records using a SOQL query: 'SELECT Id FROM Account'.

execute(): Next, in the execute() method, we’re updating each account’s name to ‘Updated Account’ and then updating the records in the database. This is the core part of our batch job.

finish(): Lastly, finish() is doing some housekeeping. Here, it’s fetching information about the job we’ve just executed (like status, number of errors, and more), and then sending an email to the job submitter with the job completion details.

That’s Batch Apex for you – a tool to handle huge volumes of data in a way that’s efficient and friendly to Salesforce governor limits. With this introduction, you’re on your way to becoming a Salesforce pro!

Next time, we’ll dive deeper into more advanced Batch Apex scenarios. Until then, practice what you’ve learned and keep exploring the wonders of Salesforce. Happy coding!

Cameron Ofoluwa
WRITTEN BY

Cameron Ofoluwa

22 Year Old Salesforce Developer @ Pogust Goodhead & Founder of SFDXHours.

Leave a Reply

Your email address will not be published. Required fields are marked *