1

I'm using an apex single email message to send an email with an attachment. Everything is working fine. But in the activity history, I can only see email body details but attachment not saved in activity.

I have checked "Always save email attachments" under "My Email to Salesforce". enter image description here

List<Messaging.SingleEmailMessage> lstsendmails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Id template_id =  [SELECT id, name FROM EmailTemplate WHERE developername ='Send_Invoice_PDF'].Id;
List<String> toAddresses = new List<String>();
toAddresses.add('[email protected]');  // e.g email address                        
mail.setToAddresses(toAddresses);
mail.setWhatId(inv); // Custom object(Invoice) id
mail.setTargetObjectId(cont.Id); // Activity save in contact
mail.setSaveAsActivity(true);
mail.setTemplateId(template_id);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); 
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(cv.Title);
efa.setBody(cv.VersionData); // File version data(blob)
fileAttachments.add(efa);
mail.setFileAttachments(fileAttachments);
lstsendmails.add(mail);

List<Messaging.SendEmailResult> sendEmailResults = Messaging.sendEmail(lstsendmails,false);

Please advise on this

1
  • Did you find a solution for this? I am facing the same issue.
    – MANUELAN00
    Dec 21, 2020 at 3:40

1 Answer 1

0

Use:

mail.setEntityAttachments(new List<Id>(/*contentVersionIds*/))

instead of:

mail.setFileAttachments()
1

You must log in to answer this question.

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