Activity Forums Salesforce® Discussions How to add existing Attachments to Visualforce Email template?

  • Satyakam

    Member
    July 27, 2016 at 6:47 am

    Hi Tanu,

    In Visualforce email template you can use below tag to render content into a PDF attachment in final email.

    <messaging:attachment renderAs="PDF" filename="fileName.pdf">
    email attachment content goes here.
    </messaging:attachment>

    But if you are trying to render an already existing PDF document from say "Documents" in salesforce, you can try below steps

    1) Get the content of the document by querying document.

    2) Assign this document body to a property(String type) in the controller of visualforce email template. Then use the property inside above mentioned tags.

    Generally if you want more control over this kind of scenario, we go for an apex trigger and from trigger we send apex generated email. In this case you can query documents or any other object or even multiple objects and control attachment contents more precisely.

    Please try below code to encode the Document body as string suitable for email attachment.It worked for me while reading text Document into apex and rendering as PDF in email,

    Public class pdfController {
    public String pdfBody{get;set;}
    public pdfController(){
    Document docu = [select body from document where Id = '01590000008P7po'];
    pdfBody = EncodingUtil.urlEncode(docu.body.toString(), 'UTF-8');

    }
    }

  • Pritha

    Member
    May 2, 2018 at 1:21 pm

    Hi Satyakam,

     

    Can you please explain the step in details. I am quote new to apex.

    1) Get the content of the document by querying document.

    2) Assign this document body to a property(String type) in the controller of visualforce email template. Then use the property inside above mentioned tags.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos