In this module, you create and execute a batch process to send reminder emails to the conference speakers.
In the Developer Console, select File > New > Apex Class, specify SendReminderEmail as the class name and click OK
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) {
}
}
Declare three instance variables to store the query, the email subject, and the email body:
global String query;
global String subject;
global String body;
Define a constructor implemented as follows:
global SendReminderEmail(String query, String subject, String body) {
this.query = query;
this.subject = subject;
this.body = body;
}
Implement the start() method as follows:
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
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);
}
}
Save the file.
Make sure you have assigned your own email address to one of the speakers
In the Developer Console, click Debug > Open Execute Anonymous Window
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);
Click Execute
Check your email