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
EvertonSzekeresEvertonSzekeres 

Schedule email

Hi,

I'm trying to send an email scheduling by a date field in my task:
global class Email_agendado_Impressao implements Schedulable {

    global void execute(SchedulableContext sc) {
    
        for (Task t: [select Id, E_mail__c, Data_do_pedido__c from Task where subject = 'Fotos faltantes - Envio de Backup']){

        if (t.Data_do_pedido__c == system.today()){

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[]{};   
            toAddresses.add(t.e_mail__c);
          mail.setToAddresses(toAddresses);
            
            mail.setSenderDisplayName('Test email');

          mail.setSubject('Test Schedule');
          mail.setPlainTextBody('Test email');
        }
        }
    }
}

I had scheduled, but I didn't receive the email.

Probably my code is wrong, it is my first time scheduling.

What should I do?

Thanks !
Best Answer chosen by EvertonSzekeres
Shyam BhundiaShyam Bhundia
missed out the send part ;)

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


All Answers

Shyam BhundiaShyam Bhundia
missed out the send part ;)

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


This was selected as the best answer
Shyam BhundiaShyam Bhundia
just to add, call the send email part outside of the for loop.  In the for loop, just contruct the Messaging.SingleEmailMessagelist.

Let me know how you get on.
EvertonSzekeresEvertonSzekeres
I can't believe...
I forgot this...haha..

Thanks Shyam, It will work now.
Shyam BhundiaShyam Bhundia
no prob, dont forget to put the send email part outside the for loop.
EvertonSzekeresEvertonSzekeres
Of course, no problem.
Thanks for warning.

Now I'm having difficulty to create a test class.
Shyam BhundiaShyam Bhundia
hehe the fun bit!