0

Created the following:

  • Workflow with alert notification using classic email template.
  • Document controller that queried Attachment object
  • Apex component to be used in classic email template visualforce page.

current workflow can have alert action that can use classic email template. then classic email template can be of visualforce page.

controller for my apex component
public with sharing class DownloadDocumentController {
:
    public Blob attachment {
        get {
            return Blob.toPdf(pdf);

        }
    }
}

---------------attachComponent
<apex:component controller="DownloadDocumentController" access="global">
    {!attachment}
</apex:component>

---------------
<messaging:emailTemplate ...
<messaging:htmlEmailBody >
...
</messaging:htmlEmailBody>
<messaging:attachment renderAs="pdf" filename="attach.pdf">
<c:attachComponent />
</messaging:attachment>
</messaging:emailTemplate>

1 Answer 1

0

I'm not sure if it can be attached with the current workflow you have in mind, but I've used Process Builder and Invocable Methods in the past to send the email template directly in the Apex code.

Here's a sample helper method that I've used in the past:

    public static Messaging.SingleEmailMessage prepEmailTemplate(String[] toAddresses, Id templateId, Id whoId, Id relatedToId) {
        Messaging.SingleEmailMessage mail = Messaging.renderStoredEmailTemplate(templateId, whoId, relatedToId);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setToAddresses(toAddresses);   // Amazing! I can set toAddresses to email templates!
        mail.setSaveAsActivity(false);      // Set to false, otherwise gives error when trying to sendEmail()
        //mail.setTreatTargetObjectAsRecipient(false);  // Don't want to send email to whoId since we're setting the toAddress manually!
        return mail;
    }

Then you can query and add the attachments using setEntityAttachments():

Map<Id, Attachment> attachments = [SELECT ... FROM Attachment WHERE ...];
Messaging.SingleEmailMessage email = prepEmailTemplate(...);
email.setEntityAttachments(attachments.keySet())`

See more on Messaging.renderStoredEmailTemplate() here

2
  • current workflow can have alert action that can use classic email template. then classic email template can be of visualforce page. Commented Apr 2, 2019 at 8:03
  • 1
    updated my question above Commented Apr 2, 2019 at 8:09

You must log in to answer this question.

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