You need to sign in to do that
Don't have an account?
ABHISHEK SINGH 237
Getting Error:Not Serializable: com/salesforce/api/fast/List$$lcom/salesforce/api/Messaging/SingleEmailMessage$$r
Below are the code of both queueable apex and batch class.
public class sendEmailsExtended implements Queueable{
private static List<Email_Log__c> emailDebugLogs = new List<Email_Log__c>();
map<String,TimeEmailRules__c> allRules;
private Boolean sendEmails = true;
public sendEmailsExtended(map<String,TimeEmailRules__c> query){
allRules=query;
}
public void execute(QueueableContext qc){
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
mail1.toAddresses=new List<String>{'abhishek.singh@yopmail.com','bkevat@yopmail.com','varun.singh@yopmail.com'};
mail1.optOutPolicy='FILTER';
mail1.subject='Subject Test Message';
mail1.plainTextBody='This is the message body.';
//Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage(toAddresses='abc1@gmail.com',optOutPolicy='FILTER',subject='Subject Test Message',plainTextBody='This is the message body.');
//Messaging.SingleEmailMessage mail3 = new Messaging.SingleEmailMessage(toAddresses='abc1@gmail.com',optOutPolicy='FILTER',subject='Subject Test Message',plainTextBody='This is the message body.');
sendEmailsBatchExtended batch = new sendEmailsBatchExtended(new List<Messaging.SingleEmailMessage>{mail1});
Id batchjobId =Database.executebatch(batch,1);
}
}
Batch class Code
global class sendEmailsBatchExtended implements Database.Batchable<sObject>,Database.Stateful{
List<Messaging.SingleEmailMessage> mailtosend;
String query;
public sendEmailsBatchExtended(List<Messaging.SingleEmailMessage> maillist){
mailtosend = maillist;
}
global Iterable<sObject> start(Database.BatchableContext bc){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<sObject> scope){
System.debug('value in mailtosend'+mailtosend);
}
global void finish(Database.BatchableContext bc){
}
}
public class sendEmailsExtended implements Queueable{
private static List<Email_Log__c> emailDebugLogs = new List<Email_Log__c>();
map<String,TimeEmailRules__c> allRules;
private Boolean sendEmails = true;
public sendEmailsExtended(map<String,TimeEmailRules__c> query){
allRules=query;
}
public void execute(QueueableContext qc){
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
mail1.toAddresses=new List<String>{'abhishek.singh@yopmail.com','bkevat@yopmail.com','varun.singh@yopmail.com'};
mail1.optOutPolicy='FILTER';
mail1.subject='Subject Test Message';
mail1.plainTextBody='This is the message body.';
//Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage(toAddresses='abc1@gmail.com',optOutPolicy='FILTER',subject='Subject Test Message',plainTextBody='This is the message body.');
//Messaging.SingleEmailMessage mail3 = new Messaging.SingleEmailMessage(toAddresses='abc1@gmail.com',optOutPolicy='FILTER',subject='Subject Test Message',plainTextBody='This is the message body.');
sendEmailsBatchExtended batch = new sendEmailsBatchExtended(new List<Messaging.SingleEmailMessage>{mail1});
Id batchjobId =Database.executebatch(batch,1);
}
}
Batch class Code
global class sendEmailsBatchExtended implements Database.Batchable<sObject>,Database.Stateful{
List<Messaging.SingleEmailMessage> mailtosend;
String query;
public sendEmailsBatchExtended(List<Messaging.SingleEmailMessage> maillist){
mailtosend = maillist;
}
global Iterable<sObject> start(Database.BatchableContext bc){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<sObject> scope){
System.debug('value in mailtosend'+mailtosend);
}
global void finish(Database.BatchableContext bc){
}
}
The message indicates that the pattern you are using won't work because of this line: with the Database.Stateful marker that tells the platform to use serialization and deserialization to hold on to the information between execute calls. For reasons I don't know, by design, Messaging.SingleEmailMessage is not serializable, hence the error message.
Instead, build the messages inside the execute method and if the overall logic is driven by a query, best to invoke that in the start method and return a QueryLocator.
Thanks,
Nagendra
Hi just Declare the List<Messaging.SingleEmailMessage> mail in global and create the instance of it in the execute method . It will work .
Use the below code for reference .
public class SendBirthdayWishesBatch implements Database.Batchable <SObject>,Database.Stateful {
public EmailTemplate eTemplate ;
public List<String> FinalEmails = new List<String>();
public List<Messaging.SingleEmailMessage> mail ;
public void execute(SchedulableContext scheduleContext){
Database.executeBatch(this);
}
public Database.Querylocator start (Database.BatchableContext BC) {
return Database.getQueryLocator(
[SELECT Name,ownerId,BirthdateForReminder__c,Active_Team_Member__c,Blumark_Email_Address__c FROM
Blumark_Team_Member__c where
Blumark_Email_Address__c != null AND
Active_Team_Member__c = true AND
BirthdateForReminder__c =: system.today()
]);
}
public void execute(Database.BatchableContext BC, List<Blumark_Team_Member__c> sendwishes){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Try {
User loggedInUser = [SELECt Email from User where Id =: userInfo.getUserId()];
eTemplate = [SELECT Id,subject,body FROM EmailTemplate where DeveloperName = 'Birthday_Email_Template'];
List<Blumark_Team_Member__c> bluMarklist = [SELECT Active_Team_Member__c,Blumark_Email_Address__c from Blumark_Team_Member__c
where Active_Team_Member__c = true AND Blumark_Email_Address__c != null AND Blumark_Email_Address__c !=: loggedInUser.Email ];
List<Blumark_Team_Member__c> todayMembersBirthday = [SELECT Blumark_Email_Address__c,BirthdateForReminder__c
from Blumark_Team_Member__c WHERE BirthdateForReminder__c =: system.today()];
for(Blumark_Team_Member__c allEmails : bluMarklist) {
for(Blumark_Team_Member__c TodayBirthdayEmail : todayMembersBirthday) {
if(allEmails.Blumark_Email_Address__c != TodayBirthdayEmail.Blumark_Email_Address__c) {
FinalEmails.add(allEmails.Blumark_Email_Address__c);
}
}
}
eTemplate = [SELECT Id,subject,body FROM EmailTemplate where DeveloperName = 'Birthday_Email_Template'];
email.setHtmlBody(eTemplate.body);
email.setSubject('BirthDay Wishes');
email.setToaddresses(FinalEmails);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
Catch(Exception e) {
System.debug('Exception' +e.getmessage());
}
}
public void finish(Database.BatchableContext bc) {
mail = new List<Messaging.SingleEmailMessage>();
Messaging.sendEmail(mail);
}
}
Make this as best answer