Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to Messaging.sendEmail() so that the email is properly shown on a record's activity tab in Lightning Experience? Asked

Asked by Dia Shrinidhi May 8, 2023 1.3K views 1 answer
Share

About this question

I want to send an Email using Messaging.sendEmail()

This Email should appear at the appropriate place in the activity tab, exactly as salesforce is placing them when sending using the LEX UI:

My current (halfway working) code looks like this

Messaging.SingleEmailMessage    m = new Messaging.SingleEmailMessage();
    m.setToAddresses(           new String[]{'heim@elastify.eu'} );
    m.setSubject(               'test' );
    m.setHtmlBody(              'whatever' );
ContentVersion                  cv = new ContentVersion();
    cv.Title                    = email.Subject+'.pdf';
    cv.PathOnClient             = cv.Title;
    cv.VersionData              = anyPdfBlobDoesNotMatterWhat;
    cv.SharingOption            = 'A'; // A : 'Allowed'
insert cv;
cv                              = [ SELECT Id, FileType, Title, ContentDocumentId FROM ContentVersion WHERE Id =: cv.Id];
ContentDocumentLink             cdl                     = new ContentDocumentLink();
    cdl.ContentDocumentId       = cv.ContentDocumentId;
    cdl.LinkedEntityId          = t.Id;
    cdl.ShareType               = 'I'; // I : 'Inferred' = The user’s permission is determined by the related record.
insert cdl;
Messaging.EmailFileAttachment   efa = new Messaging.EmailFileAttachment();
    efa.setFileName(cv.Title);
    efa.setBody( pdfBlob );
    efa.setContentType('application/pdf'); // does not work: (cv.FileType);
    efa.setInline(false);
m.setFileAttachments( new Messaging.EmailFileAttachment[]{ efa });
if(!(Test.isRunningTest())) {
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { m });
} 

Only that I can not link the sent email to a given Record, let's say there is an SObject MyObject__c record, queried with a given Id. The id is available as record.Id

How can I connect record or record.Id to the email sent above??

Assuming that again like here we have to magically and undocumented glue that together using junction task, following a recipe like:

Create a wrapper/junction Task

Link the EmailMessage somehow via ActivityId to the wrapper Task

Only that seems not to be possible. Again.

Any ideas?

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.