Is it possible to save an Email template as a PDF attachment to a record ?


@Srujan Sujju Yes, It's possible refer below code.

// Construct the list of emails we want to send
    List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
    msg.setTemplateId( [select id from EmailTemplate where Name='abc'].id );

    msg.setWhatId('your WhatId');
    msg.setTargetObjectId('Target object record id');
    msg.setToAddresses(new List<String>{'noreplay@abc.com'});
    msg.saveAsActivity = false;
    // Send the emails in a transaction, then roll it back
    Savepoint sp = Database.setSavepoint();
    // For each SingleEmailMessage that was just populated by the sendEmail() method, copy its
    // contents to a new SingleEmailMessage. Then send those new messages.
    string body = lstMsgs[0].getHTMLBody();

    Attachemnt att = new Attachment();
    att.body = body;
    att.ParentID = "Your Record Id"
    insert att.
  • What type of Email Template can I use ? Will it work will all types ? – Srujan Nov 25 '16 at 6:12
  • is it gethtmlbody or sethtmlbody ? – Srujan Nov 25 '16 at 6:19
  • string body = msg.getHTMLBody(); the body is always null . – Srujan Nov 25 '16 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.