You need to sign in to do that
Don't have an account?
Prateek 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:
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
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 = System.now()-1; 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}); email.setSaveAsActivity(false); email.setPlainTextBody(body); email.setTemplateId(emailTemplate.Id); system.debug('Checkemails' + email); //Here i am getting problem mails.add(email); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); } } global void finish(Database.BatchableContext BC) { // for sending final emails: Messaging.sendEmail(mails); } }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
Could you please try moving initialization of public "List<Messaging.SingleEmailMessage> mails" in constructor of class?
Regards,
Prasanna
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
Can you please provide some saample code for starting this off, that will be really good help here, thanks