Send Visualforce as an email attachment from Apex Trigger – Alternate design

Process Builder & InvocableMethod- Alternative to send Visualforce as an email attachment from Apex Trigger

Send Visualforce as an email attachment from Apex Trigger

Previously I had written same blog post and it was working fine before Winter 16. However, after Winter 16 critical update was released by Salesforce to consider getContent() method as a callout. If this update is enabled in your Salesforce instance then my previous blog post will not work.

In this blog post, we will go through alternate design, where we would still be able to send Visualforce page content as an Email attachment. Difference is, instead of Apex Trigger , Invocable Method and Process builder will help us getting there.

Again, consider below simple Visualforce page, which we want to send as an attachment.


<apex:page renderAs="PDF" > 

<h1 >Congratulations </h1 >
  This is your new Page 

Below is Apex class containing Invocable method to be executed from Process builder

 * 	Author		:	Jitendra Zaa
 * 	Date		:	Aug 26 2016
 * 	Description	:	Method in this class is invoked by Process builder to send an Email
 * */
public class SendVF_Email_InvocableMethod {

    public static void sendEmail(List<Id> lstId){
        List<String> EmailIds = ''.split(',');

        PageReference ref = Page.PDF_DEMO;
        Blob b = ref.getContentAsPDF();

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();

        String addresses;
        email.setSubject( 'Check VF From PB' +String.valueOf(;
        email.setToAddresses( EmailIds  );
        email.setPlainTextBody('Hey there, I am an email Body');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Last but not the least, we need to create a process builder and invoke above method in same situation, Trigger could have executed it. We can always use some hidden fields in trigger and populate for Process builder so that decision making would be easy.

Don’t forget to leave your comments and feedback for other possible designs or suggestions.

Related posts