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
    Commented Dec 21, 2020 at 3:40

2 Answers 2

1

Salesforce stores files inside ContentVersion and its controllability is handled parent record of ContentDocumentLink.

To attach the file with a record you have to create a ContentDocumentLink record. ContentDocumentLink's LinkedEntityId field stores the ID of the record to which you want to link the attachment.

So you can not have multiple parents of the same file.

To resolve this, you must create a new ContentVersion and a new ContentDocumentLink record.

Refer to the below code -

List<Messaging.SingleEmailMessage> lstsendmails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Id template_id =  [SELECT id, name FROM EmailTemplate WHERE developername ='SupportSelfServiceResetPassword'].Id;
List<String> toAddresses = new List<String>();
toAddresses.add('[email protected]');  // e.g email address                        
mail.setToAddresses(toAddresses);
//mail.setWhatId('0035j000016vu84AAA'); // Custom object(Invoice) id
mail.setTargetObjectId('0035j000016vu84AAA'); // 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();
ContentVersion cv = [SELECT id,title,VersionData,FileType FROM contentversion where id='0685j00000MDaxaAAD'];
ContentVersion c1 = new ContentVersion();
c1.Title = cv.title;
c1.VersionData = cv.VersionData;
c1.PathOnClient = '/' + cv.title;
insert c1;
ContentVersion c2 = [SELECT Id, Title, ContentDocumentId,versiondata
                     FROM ContentVersion WHERE Id = :c1.Id LIMIT 1];
efa.setFileName(c2.Title);
efa.setBody(c2.VersionData); // File version data(blob)
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
lstsendmails.add(mail);

List<Messaging.SendEmailResult> sendEmailResults = Messaging.sendEmail(lstsendmails,false);
system.debug(sendEmailResults[0]);
ContentDocumentLink cl = new ContentDocumentLink();
cl.ContentDocumentId = c2.ContentDocumentId;
cl.LinkedEntityId = [SELECT Id FROM EmailMessage ORDER BY CreatedDate desc LIMIT 1].Id;
cl.ShareType = 'V';
cl.Visibility = 'AllUsers';
insert cl;

I'm querying the EmailMessage as per the screenshot you want to attach file here.

-1

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 .