+ Start a Discussion
Sid LightningSid Lightning 

Can I save email template as pdf under attachements related list of Opportunity


I want to save attachement of the email template that gets sent via batch apex to the user email.

I want to save it under Attachements under Opportunities, can someone suggest how can I do this 
Deepali KulshresthaDeepali Kulshrestha
Hi Sid,

I've gone through your requirement and you can refer below code:
Apex Code-->

   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 Opportunity Record Id"
    insert att.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Sid LightningSid Lightning
Hi Deepali,

I am sending email template via workflow email alert. Can the same code apply there