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
AksharaAkshara 

apex scheduler

Hi all,

My requirement is to send an email to all the Active accounts on the 1st of every month.I have to use apex Scheduler since it is not possible to do with a workflow.

I started with the below skeleton code and I want to test this using execute anonymous window.

Global class SendEmailAccount implements Schedulable
{
       global void execute(SchedulableContext sc)
                        {
                                    sendmail();
                        }
 
                        public void sendmail()
                        {// Get default sender email address   
                            OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
                            EmailTemplate templateId = [Select id from EmailTemplate where name = 'TemplateName’];
                            List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
                            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                            mail.setTemplateID(templateId.Id); 
                            mail.setToAddresses(new String[] { ‘Senderemailaddress’});
                            mail.setSaveAsActivity(false);
                            mail.setOrgWideEmailAddressId(owa.id);
                            allmsg.add(mail);
                            System.debug('inside sendmail method');
                            if(allmsg.size()>0)
                            {
                             Messaging.sendEmail(allmsg,false);
                              System.debug('inside send mesage method');
                            }
                            
                        }
                                               
}


I used below code in anonymous window

SendEmailAccount  SEA= new SendEmailAccount();
                SEA.execute(null);
The code is getting executed but I don't see any email.Any help would be appreciated.Also, can someone let me know how to dynamically insert sender's email address with account email address?
Sure@DreamSure@Dream
HI Akshara

You cant have more than 10 Messaging.sendEmail() calls in a single transaction.
Check the debug logs. 
AksharaAkshara
Hi Sure@Dream,

Thanks for your input.However,I am making only one call to Messaging.sendEmail()  in this transaction.I don't see any error in the debug logs either
AksharaAkshara
I think I will have to set TargetObjectId