Although I've defined setPlainTextBody, it throws the error: First error: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Email body is required.: []
global class OppStatusNotification_Batch implements Database.Batchable{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, Name, StageName, Owner.Email from Opportunity';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
String messageBody;
for(Opportunity opp : (List<Opportunity>)scope){
if(opp.StageName == 'Submitted to Manager'){
messageBody = 'This application was submitted to Manager.';
}
else if(opp.StageName == 'Declined by Manager'){
messageBody = 'This application was declined by Manager'.;
}
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {opp.Owner.Email};
email.setToAddresses(toAddresses);
email.setSubject('Opportunity Name: ' + opp.Name);
email.setPlainTextBody(messageBody);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
global void finish(Database.BatchableContext BC)
{
}
}