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
baller4life7baller4life7 

Sending many single emails with 1 sendEmail() execution

 

global class OpportunityStatusReport implements Schedulable {
 
    public static final String EMAIL_TEMPLATE_ID = '00XP0000000HucQ';
    public List<AggregateResult> openOpps = new List<AggregateResult>();
    public Messaging.SingleEmailMessage [] singleEmails = new List<Messaging.SingleEmailMessage>();
    public Messaging.Email[] allEmails = new List<Messaging.Email>();
    
  
    global void execute (SchedulableContext SC)
    {
        // Gets the list of the opportunity status report recipients. Only active users with open opportunities will get an email.
        openOpps = 
        [
            SELECT o.OwnerId 
            FROM Opportunity o
            WHERE o.Owner.isActive = true
            AND o.isClosed = false
            GROUP BY o.OwnerId
        ];
        
        if(openOpps.size() > 0)
        {
            // Creates a personalized email for each recipient
            for (AggregateResult opp : openOpps)
            {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId((String)opp.get('OwnerId'));
                mail.setTemplateId(EMAIL_TEMPLATE_ID);
                mail.setSaveAsActivity(false);
                singleEmails.add(mail);
            }
            
            for( Integer i = 0; i < singleEmails.size(); i++ )
            {
                allEmails.add(singleEmails.get(i));
            }
            
            if (allEmails.size() > 0)
            {
                Messaging.sendEmail( allEmails );
            }
        }
    }
}

 

 

System.UnexpectedException: Not Serializable: LIST<Messaging.Email>

 

---

 

Hey guys,

how can I fix the exception? (Making the list transient doesn't solve the problem)

 

Thx for your help

Josh

Best Answer chosen by Admin (Salesforce Developers) 
sham_1sham_1

Hi Josh,

 

I believe moving the

public Messaging.SingleEmailMessage [] singleEmails = new List<Messaging.SingleEmailMessage>();
    public Messaging.Email[] allEmails = new List<Messaging.Email>();

variables inside the execute block , will solve your problem.

Then the variables, are destroyed after the execute method returns and need not be persisted across invocations.

 

All Answers

Ankit AroraAnkit Arora

Hi Josh,

 

Not sure if we can resolve your issue, but if you want to send emails from batch then I have posted some code here :

http://forceguru.blogspot.com/2011/03/how-to-send-more-than-10-e-mails.html

 

Hope this will help.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

sham_1sham_1

Hi Josh,

 

I believe moving the

public Messaging.SingleEmailMessage [] singleEmails = new List<Messaging.SingleEmailMessage>();
    public Messaging.Email[] allEmails = new List<Messaging.Email>();

variables inside the execute block , will solve your problem.

Then the variables, are destroyed after the execute method returns and need not be persisted across invocations.

 

This was selected as the best answer
baller4life7baller4life7

Thanks guys! Sham_1's solution worked for me. I also had to remove the public keywords...