You need to sign in to do that
Don't have an account?
Michael 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});
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});
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.