function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
niven sfniven sf 

Hi! I am this error in apex batch class. Error is : First error: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Email body is required.: []. Please help me to solve this.

Pradeep SinghPradeep Singh
Hi, If you have written some code to send emails , then you have missed the body of the email.

example:-
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { '003D000000QDexS', '003D000000QDfW5' };
message.optOutPolicy = 'FILTER';
message.subject = 'Opt Out Test Message';
message.plainTextBody = 'This is the message body.';    // you can also use message.setPlainTextBody(plainTextBody)​
Messaging.SingleEmailMessage[] messages = 
    new List<Messaging.SingleEmailMessage> {message};
         Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
if (results[0].success) {
    System.debug('The email was sent successfully.');
} else {
    System.debug('The email failed to send: '
          + results[0].errors[0].message);
}
 
niven sfniven sf
Hello Pradeep,

Thanks for your reply. I have another error in batch class.

First error: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, Subscript is invalid because list is empty: []
Pradeep SinghPradeep Singh
can you please post your code..
niven sfniven sf
global class RemainderEmailBatch implements Database.Batchable<sObject>, Database.Stateful {
    private String query;
   
        
    global Database.QueryLocator start(Database.BatchableContext BC){ 
    
          
        
        query = 'select id,Name,Begin_Date__c,Activity_Branch__C,Ownerid,Email__c,Activity_Type__C,Member__r.id,days__c,Primary_Transaction_Type__C,Camper_Key__c,CancelRegistrations__c,Paper_Work__C,Waiver_Form__c,checked__C,Medication_Form__C from PGMReg__c';
         
        system.debug('query'+query);
        return Database.getQueryLocator(query);
    }
  
    global void execute(Database.BatchableContext BC, List<PGMReg__c> scope)

    {   
         
           
        List<Messaging.SingleEmailMessage> emailmsglist = new List<Messaging.SingleEmailMessage>();
        for(PGMReg__C p: scope)

        {
       
            if(  p.Activity_Branch__c == '210' && (p.Activity_Type__c == 21 || p.Activity_Type__c == 22) && p.Camper_Key__C != null && p.Primary_Transaction_Type__c == '' && p.CancelRegistrations__C == false && p.Paper_Work__c ==false){

                List<String> toemails = new List<String>();

                 Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();
                 OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'johncabry2018@gmail.com'];
           
             if ( owea.size() > 0 ) {
          email.setOrgWideEmailAddressId(owea.get(0).Id);
          }

                 toemails.add(p.Email__c);
                  system.debug('toemails'+toemails);
                        
                    email.setTemplateId('00Xm0000000Ix4Y');
                    system.debug('emailtemplate'+email);
                    email.setToAddresses(toemails);
                String[] ccAddresses = new String[] {'nivensf@gmail.com'};
                    email.setBccAddresses(ccAddresses);
                   
                   email.setTargetObjectId(p.Ownerid);
                  
                    email.setsaveAsActivity(false);

                    emailmsglist.add(email);

            }

            
        }  
        
         if(emailmsglist.size()>0){

            Messaging.sendEmail(emailmsglist) ; 
         }
       
}           
        
 
    
global void finish(Database.BatchableContext BC){
 
 }
}
niven sfniven sf
sending visualforce email template in batchclass is possible or not