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
Michael MMichael M 

Can I trigger an apex class to send an email 3 days after a ListEmail is sent?

Hello, I have apex code that collects the EmailStatus for listemails tha thave been sent. I would like to trigger this to send 3 days after a ListEmail is sent. Is that possible, and if so how?

Here is my code, if this helps to visualize what I am talking about:
// fetch Data using SOQL from Salesforce
List<Contact> contList = [Select id, name, CreatedDate, lastModifiedDate, email,
                           (SELECT  id, taskId, createdDate, firstOpenDate, lastOpenDate, timesOpened, emailTemplateName, task.subject, task.TaskSubtype
                          FROM EmailStatuses where createddate = last_month and task.tasksubtype = 'ListEmail')
                          from Contact where email != null ];
//Set Header values of the file
string csvHeader = 'Contact Name, Contact Email    , Subject, Date Sent, Times Opened, Task Subtype \n';
string mainContent = csvHeader;
for (Contact cnt: contList){
    for (emailstatus es : cnt.EmailStatuses){
        integer month = es.createddate.month();
        integer year = es.createddate.year();
    integer day = es.createddate.day();
        if (month == 12 && year == 2020 && day == 8){
      //Adding records in a string
       string recordString = cnt.name+','+cnt.email +','+es.task.subject +','+es.CreatedDate+','+es.timesOpened +','+es.task.tasksubtype +'\n';
       mainContent += recordString;
        }}}
Messaging.EmailFileAttachment csvAttcmnt = new Messaging.EmailFileAttachment ();

//Create CSV file using Blob
blob csvBlob = Blob.valueOf (mainContent);
string csvname= 'Contact.csv';
csvAttcmnt.setFileName (csvname);
csvAttcmnt.setBody (csvBlob);
Messaging.SingleEmailMessage singEmail = new Messaging.SingleEmailMessage ();
String [] toAddresses = new list<string> {'me@comp.org'};
//Set recipient list
singEmail.setToAddresses (toAddresses);
String subject ='Contact CSV';
singEmail.setSubject (subject);
singEmail.setPlainTextBody ('Contact CSV');

//Set blob as CSV file attachment
singEmail.setFileAttachments (new Messaging.EmailFileAttachment []{csvAttcmnt});
Messaging.SendEmailResult [] r = Messaging.sendEmail (new Messaging.SingleEmailMessage [] {singEmail});
Best Answer chosen by Michael M
AbhishekAbhishek (Salesforce Developers) 
https://developer.salesforce.com/forums/?id=906F0000000BancIAC

This one might guide you.


If it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.