Apex Batch and Scheduled Apex  

Apex Batch and Scheduled Apex

What is Batch Apex?

Batch Apex is used when you want to process a large number of records (more than 50,000) without hitting governor limits. Salesforce breaks your job into small chunks (called batches), each processed separately. 

Why Use Batch Apex?

To process millions of records without governor limit errors.
To automate big jobs like data cleanup, report updates, etc.
To process records asynchronously in the background.

Basic Structure of a Batch Class

You must implement the Database.Batchable interface, which includes: 

apex 

global class YourBatchClass implements Database.Batchable<SObject> { 

 

    global Database.QueryLocator start(Database.BatchableContext bc) { 

        // Query records to process 

    } 

 

    global void execute(Database.BatchableContext bc, List<SObject> scope) { 

        // Logic for each batch of records 

    } 

 

    global void finish(Database.BatchableContext bc) { 

        // Optional – final actions 

    } 

} 

Example 1: Update Contact Description Field

Business Logic:

We’ll update all Contact records and set their Description field to “Updated by Batch”. 

apex 

global class ContactBatchUpdate implements Database.Batchable<SObject> { 

 

    global Database.QueryLocator start(Database.BatchableContext bc) { 

        return Database.getQueryLocator(‘SELECT Id, Description FROM Contact’); 

    } 

 

    global void execute(Database.BatchableContext bc, List<SObject> scope) { 

        for(SObject s : scope) { 

            Contact c = (Contact)s; 

            c.Description = ‘Updated by Batch’; 

        } 

        update scope; 

    } 

 

    global void finish(Database.BatchableContext bc) { 

        System.debug(‘Batch process completed!’); 

    } 

} 

To Run:

apex 

ContactBatchUpdate batch = new ContactBatchUpdate(); 

Database.executeBatch(batch, 200); // 200 = batch size 

Code Explanation:

start(): Gets all Contact records.
execute(): Loops over each batch and updates the Description.
finish(): Logs a completion message.

Expected Output:

Every contact’s description will be updated to: “Updated by Batch”. 

What is Scheduled Apex?

Scheduled Apex lets you run Apex code at a specific time – daily, weekly, monthly, or even hourly. 

Structure of a Scheduled Class

apex 

global class YourSchedulerClass implements Schedulable { 

    global void execute(SchedulableContext sc) { 

        // Logic to run 

    } 

} 

Example 2: Schedule the Contact Batch Job

We’ll create a schedule class to run the batch daily at 7:00 AM. 

Scheduled Class:

apex 

global class ContactBatchScheduler implements Schedulable { 

 

    global void execute(SchedulableContext sc) { 

        ContactBatchUpdate batch = new ContactBatchUpdate(); 

        Database.executeBatch(batch, 200); 

    } 

} 

To Schedule the Job:

apex 

String cronExp = ‘0 0 7 * * ?’; // Every day at 7 AM 

String jobName = ‘Daily Contact Update Batch’; 

ContactBatchScheduler sch = new ContactBatchScheduler(); 

System.schedule(jobName, cronExp, sch); 

 

Cron Expression Breakdown:

Position Value Meaning
1 0 Seconds
2 0 Minutes
3 7 Hours (7 AM)
4 * Day of month
5 * Month
6 ? Day of week

Expected Output:

The Contact batch will automatically run daily at 7 AM and update all contact descriptions. 

Best Practices

Use meaningful names for batch/scheduler classes.
Limit SOQL queries and DML inside execute.
Use Test.startTest() and Test.stopTest() in test methods.
Always handle errors gracefully (try/catch).

Schedule jobs responsibly (avoid job overload).

Test Class Template

For every batch class, write a test like this: 

apex 

@isTest 

private class ContactBatchUpdateTest { 

     

    @isTest 

    static void testBatchExecution() { 

        // Create sample contacts 

        List<Contact> contacts = new List<Contact>(); 

        for (Integer i = 0; i < 10; i++) { 

            contacts.add(new Contact(LastName=’Test’+i)); 

        } 

        insert contacts; 

 

        Test.startTest(); 

        ContactBatchUpdate batch = new ContactBatchUpdate(); 

        Database.executeBatch(batch, 5); 

        Test.stopTest(); 

 

        for (Contact c : [SELECT Description FROM Contact]) { 

            System.assertEquals(‘Updated by Batch’, c.Description); 

        } 

    } 

} 

Practice Tasks

1. Create a batch class that: 

  • Finds all Account records where Industry is blank. 
  • Updates the Industry to “Unknown”. 

2. Create a batch class that: 

  • Deletes all Lead records older than 1 year (based on CreatedDate). 

3. Create a schedulable class that: 

  • Schedules the batch from Task 1 to run every Monday at 6:00 AM. 

4. Create a batch class that: 

  • Sends an email to the owner of each Opportunity with Stage = ‘Closed Lost’. 

 
5.  Create a batch class to update account rating to “hot”  

of all accounts which have annual revenue is more than $3500,000

6. Create a batch class to append account name and opportunity name and store it on a opportunity field

7. Write a batch class to Update Opportunity Stage to “Closed Lost” if the close date is in the past and it’s not already closed. 

 

8. Write a batch class to Automatically update Account Rating based on AnnualRevenue. 

Logic: 

Revenue > 1M → Rating = ‘Hot’  

Revenue 500K–1M → ‘Warm’ 

< 500K → ‘Cold’