Module 12: Batch and Schedule

In this module, you create and execute a batch process to send reminder emails to the conference speakers.

Step 1: Create the Batch Class

  1. In the Developer Console, select File > New > Apex Class, specify SendReminderEmail as the class name and click OK

  2. Make the class global, implement the Batchable interface, and define the three methods of the interface:

    global class SendReminderEmail implements Database.Batchable<sObject> {
    
        global Database.QueryLocator start(Database.BatchableContext bc) {
    
        }
    
        global void execute(Database.BatchableContext bc, List<Speaker__c> scope) {
    
        }
    
        global void finish(Database.BatchableContext bc) {
    
        }
    
    }
    
  3. Declare three instance variables to store the query, the email subject, and the email body:

    global String query;
    global String subject;
    global String body;
    
  4. Define a constructor implemented as follows:

    global SendReminderEmail(String query, String subject, String body) {
        this.query = query;
        this.subject = subject;
        this.body = body;
    }
    
  5. Implement the start() method as follows:

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }
    
  6. Implement the execute() method as follows:

    global void execute(Database.BatchableContext bc, List<Speaker__c> scope) {
        for (Speaker__c speaker : scope) {
            EmailManager.sendMail(speaker.Email__c, this.subject, this.body);
        }
    }
    
  7. Save the file.

Step 2: Run the Batch

  1. Make sure you have assigned your own email address to one of the speakers

  2. In the Developer Console, click Debug > Open Execute Anonymous Window

  3. Type the following Apex code:

    String q = 'SELECT First_Name__c, Last_Name__c, Email__c FROM Speaker__c WHERE Email__c != null';
    SendReminderEmail batch = new SendReminderEmail(q, 'Final Reminder', 'The conference starts next Monday');
    Database.executeBatch(batch);
    
  4. Click Execute

  5. Check your email

comments powered by Disqus