1

i want to write email alert through apex scheduler , i wrote some code but when i schedule it does not run.

global class EmailSenderfriday implements schedulable {
    global void execute(SchedulableContext sc)
     {
     EmailSenderfriday e1 = new EmailSenderfriday();
     e1.sendemail();
     }
    global void sendemail (){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(getEmailAddresses());
    mail.setSubject('Check Sales Opportunity Update for Next Week Email');
    mail.setPlainTextBody('Dear MS<br><br> Kindly check all  sales opportunities for next week. Ignore if already done.<br><br><br>Regards<br>CRM Administrator');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

    private List<String> getEmailAddresses() {
    List<String> idList = new List<String>();
    List<String> mailToAddresses = new List<String>();
    Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'MY GROUP'];
    for (GroupMember gm : g.groupMembers) {
    idList.add(gm.userOrGroupId);
    }
    User[] usr = [SELECT email FROM user WHERE id IN :idList];
    for(User u : usr) {
    mailToAddresses.add(u.email);
    }
    return mailToAddresses;
    }
}
7
  • Hi Abhishek, welcome to SFSE. Please take a moment to visit the help center, scroll through the tour, and read How to Ask. This question does not contain enough detail to be answerable. Please edit it to clarify what behavior you observe, what investigative steps you have already carried out, and what you know so far.
    – Adrian Larson
    Commented Jun 5, 2017 at 19:31
  • What settings to you have on Setup->Email Administration->Deliverability in Access to Send Email (All Email Services) section? Commented Jun 5, 2017 at 19:31
  • Make sure you have data in your org. Try run this statement in Apex window and see you have users in My Group "Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'MY GROUP'];"
    – SL man
    Commented Jun 5, 2017 at 20:13
  • FYI, do not use setToAddresses to email users. This will waste governor limits, and you may not be able to send all the emails once you reach the limit. setTargetObjectId should be used instead.
    – sfdcfox
    Commented Jun 5, 2017 at 22:18
  • Hii alexander on deliverability in access to send email is(system email only) is salected @AlexanderBerehovskiy Commented Jun 6, 2017 at 6:28

1 Answer 1

1

Is is possible to send emails via Apex with only "All email" value of Access level. So go to Setup->Email Administration->Deliverability in Access to Send Email (All Email Services) section and change it to "All email"

No access: Prevents all outbound email to and from users.

System email only: Allows only automatically generated emails, such as new user and password reset emails. Especially useful for controlling email sent from sandboxes so that testing and development work doesn’t send test emails to your users. Newly created sandboxes default to System email only. All email: Allows all types of outbound email. Default for new, non-sandbox organizations. Sandboxes created before Spring ’13 default to All email.

2
  • That I also done but not working Commented Jun 6, 2017 at 11:56
  • Did you check the email logs ?
    – SL man
    Commented Jun 6, 2017 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .