You need to sign in to do that
Don't have an account?
![Akshara Akshara](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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?
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?
You cant have more than 10 Messaging.sendEmail() calls in a single transaction.
Check the debug logs.
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