Send Visualforce as an email attachment from Apex Trigger

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

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


4 responses to “Send Visualforce as an email attachment from Apex Trigger – Alternate design”

  1. Uvais Komath Avatar
    Uvais Komath

    I am using the same with visualforce template but i am getting an email exception that the toaddress is null.
    I am using a visualforce template for this

  2. INDIRA DAS Avatar

    thank u for sharing

  3. krian Avatar

    Hi ,
    I have a requirement like this ,I have a custom object which related to account,opportunity when a value of a field in custom object is changed from one to another i need to send an email with pdf as attachment.

    The attached pdf should contains 4 tables :
    1)Account Information
    2)Custom Object Information
    3)Opportunity Information
    4)Opportunity Product Information

    Can you help me how to achieve this i already tried but getting desired result.


  4. Rohan Saraf Avatar
    Rohan Saraf

    Hi jitendra..
    Thank you very much for you tremendous effort in sharing the knowledge with us.
    Actually I refered your above code for my requirement.

    My requirement is that I wanted to attach the Pdf of the invoice for the booking record. Works fine and and also the PDF gets attached and sent but the major Problem is “THE PDF COMES OUT TO BE BLANK” nothings is displayed in it.

    I also assigned the respective permissions and access to it but still the error persists..!!!
    could you please help me..!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading