0

When i'm trying to use the singleEmailMessage i get the following error:

System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Email body is required.

However in my code(below) i specify the email body, what am i missing here?

   public void finish(Database.BatchableContext bc){

        String[] toAddresses = new String[] {'[email protected]'}; //System Admin E-mail
        String emailBody;

          if(!errors.isEmpty()) {
            System.debug('notEmpty');  
            for(Exception error : errors)
            {
                emailBody += error + '\n';
            }
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSubject('Errors occurred during batch process.');
            mail.setToAddresses(toAddresses);
            mail.setSaveAsActivity(false);
            mail.setPlainTextBody(emailBody);

            Messaging.sendEmail(new Messaging.Email[] { mail });
        }
    }

1 Answer 1

0

errors is empty and to resolve your issue you need to declare emailBody variable as String emailBody='';

correct code :

public void finish(Database.BatchableContext bc){

    String[] toAddresses = new String[] {'[email protected]'}; //System Admin E-mail
    String emailBody='';

      if(!errors.isEmpty()) {
        System.debug('notEmpty');  
        for(Exception error : errors)
        {
            emailBody += error + '\n';
        }
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('Errors occurred during batch process.');
        mail.setToAddresses(toAddresses);
        mail.setSaveAsActivity(false);
        mail.setPlainTextBody(emailBody);

        Messaging.sendEmail(new Messaging.Email[] { mail });
    }
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .