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
Prateek Jain 52Prateek Jain 52 

Getting Serialize Exception in Apex Batch When trying to Send bulk emails from finish Method

HI All, 

I am trying to write simple batch class in which i have send multiple emails from the finish method of batch class , Please see the below code that i am trying to use: 
global class NotifyBatch2 implements Database.Batchable<sObject>, Database.Stateful {

datetime dt =;
public string query = 'select Id, name,Status__c,CreatedDate,Materials_Meeting_Date__c,Materials_Due_Date__c,CreatedBy.Email,Submitter__c,Book_Coverage__c,Notes__c,Materials_Request_Organization__c from MR_Product__c where Status__c=\'Pending Submission\'';

//Here i am getting the problem.
public List<Messaging.SingleEmailMessage>  mails = new List<Messaging.SingleEmailMessage>();

global database.querylocator start(Database.BatchableContext BC)

    return Database.getQueryLocator(query);

global void execute(Database.BatchableContext BC, Sobject[] scope)

    EmailTemplate emailTemplate = [select Id, Body  from EmailTemplate where DeveloperName = 'BOOK_REQUEST_Template'];

    for (MR_Product__c  item : (List<MR_Product__c>)scope) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
             string MatDueDate = string.valueOfGmt(item.Materials_Due_Date__c);
             string MatMeetingDate = string.valueOfGmt(item.Materials_Meeting_Date__c);      
                    string body = emailTemplate.body;
                    body  = body.replace('{!MR_Product__c.Submitter__c}', item.Submitter__c);
                    body  = body.replace('{!MR_Product__c.Materials_Request_Organization__c}', item.Materials_Request_Organization__c);
                    body  = body.replace('{!MR_Product__c.Materials_Due_Date__c}', MatDueDate );
                    body  = body.replace('{!MR_Product__c.Materials_Meeting_Date__c}', MatMeetingDate);
                    if(item.Notes__c!=null && item.Notes__c!=''){
                    body  = body.replace('{!MR_Product__c.Notes__c}', item.Notes__c);
                     if(item.Book_Coverage__c!=null && item.Book_Coverage__c!=''){
                    body  = body.replace('{!MR_Product__c.Book_Coverage__c}', item.Book_Coverage__c);

                    email.setToAddresses(new String[] {item.createdby.Email});
                    system.debug('Checkemails' + email);
					//Here i am getting problem
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

global void finish(Database.BatchableContext BC) {

    // for sending final emails:


Error i am getting : 

First error: Not Serializable: com/salesforce/api/fast/List$$lcom/salesforce/api/Messaging/SingleEmailMessage$$r

I have already tried making it as transient with no sucess, 

All i want is to send Emails for all records "created by user " in finish method i single go, plaese help , thanks

Hi Prateek,

Could you please try moving initialization of public "List<Messaging.SingleEmailMessage>  mails" in constructor of class? 

Prateek Jain 52Prateek Jain 52
Hi Prasu, 

Thanks for yuor quick response but still giving the same error even after moving it to consturtor , please see below code that i tried.

public MaterialRequestNotifyBatch2(){

mails = new List<Messaging.SingleEmailMessage>();


Any suggestions, thanks
Then may be you will need to create a List of inner class object which hold all email information and then map them to SingleEmailMessage in Finish method.
Prateek Jain 52Prateek Jain 52
Hi Prasu, 

Can you please provide some saample code for starting this off, that will be really good help here, thanks