function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JJamesJJames 

Send Visualforce Email Template as PDF Attachment

I have a custom visualforce Email Template and want to email it but as an attachment.  Is this possible to do or what would be my best approach?  I could just create a regular visual force page and render as pdf I know but wondering if I can salvage the EmailTemplate in any way and send it as an attachment.  Thoughts??
JJamesJJames
Still need help, I found a method in blob that i used below, but for some reason the getHtmlBody returns null on my piece of mail:
 
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        
        String[] toAddresses = new String[] {'example@sfdc,com'};
        mail.setToAddresses(toAddresses);
        EmailTemplate template = [Select id from EmailTemplate where name=:'testing visualforce template'];
        system.debug(template);
        

        Messaging.EmailFileAttachment attachmentFile = new Messaging.EmailFileAttachment();
	attachmentFile.setFileName('TestPDF.pdf'); // neat - set name of PDF
        
        
        mail.setTemplateId(template.id);

        Opportunity opp = [SELECT id, OwnerId from Opportunity where id=:jo.Opportunity__c]; 

        mail.setTargetObjectId(opp.OwnerId);
        mail.setWhatId(jo.id);

        mail.saveAsActivity = false;

        system.debug(mail);    
        String mailString = 'Testing';//mail.getHtmlBody();
        system.debug(mailString);
        attachmentFile.setBody(blob.toPdf(mailString));

and after that i send out the email, and i get the email as I should, the email template data, but there is nothing in the getHtmlBody or getPlainTextBody fields of the mail object.  how do i pull the data from the mail object that it received from the email template?
LakshmanLakshman
are you using  a static attachment or it has merge fields (dynamic) ?
JJamesJJames
It is dynamic.
LakshmanLakshman
You can make use of Visualforce Components inside your template to achieve this, below is a sample code:
Component Code:
<apex:component controller="OpptyProductController" access="global">
 <apex:attribute access="global" assignTo="{!opptyId}" name="opportunityId" type="String" required="true" description="Opportunity Id"/>
 <b>products Details</b>
 <br/>
 <br/>
 <table border="1" cellSpacing="0" cellpadding="0" width="100%" >
 <tr>
 <th>Product Name</th>
 <th>Quantity</th>
 <th>Sales Price</th>
 <th>Date</th>
 <th>Description</th>
 </tr>
 <apex:repeat value="{!OpptyProducts}" var="product">
 <tr>
 <td>{!product.PricebookEntry.Product2.Name}</td>
 <td>{!product.Quantity}</td>
 <td>{!product.UnitPrice}</td>
 <td>{!product.ServiceDate}</td>
 <td>{!product.Description }</td>
 </tr>
 <br/>
 </apex:repeat>
 </table>
 </apex:component>

Sample Controller:
Public Class OpptyProductController {
   public String opptyId{get;set;} /** get set the variable for opportunity Id take value from visual component.**/
   public List<OpportunityLineItem> getOpptyProducts() {
        return [Select Quantity, UnitPrice, ListPrice,ServiceDate, Description ,PricebookEntry.Product2.Name
                From OpportunityLineItem where OpportunityId =: opptyId];
   }
}

Finally the VF template:
<messaging:emailTemplate subject="Opportunity '{!relatedTo.Name}' Detail with Products" recipientType="User"
        relatedTo="opportunity >
<messaging:htmlEmailBody >
Hi {!relatedTo.Owner.name} ,<br/>
your opportunity '{!relatedTo.Name}' has beed closed won.<br/>
Please find their products detail as attachment.
</messaging:htmlEmailBody>
 
<messaging:attachment renderAs="pdf"> <!-- Use the renderAs attribute here.-->
    <c:OpptyProduct opportunityId="{!relatedTo.Id}"></c:OpptyProduct><!--gives the reference of the component-->
</messaging:attachment>
 
</messaging:emailTemplate>
Trigger the above stuff as below:
Id templateId = 'place_your_vf_template_id';
Id someOpportunity = 'place_your_record_id';
Messaging.singleEmailmessage em = new Messaging.singleEmailMessage();
em.setTemplateId(templateId);
em.setTargetObjectId(UserInfo.getUserId());
em.setWhatId(someOpportunity);
em.setSaveAsActivity(false);
em.setToAddresses( new List<String>{ 'example@example.org' } );
Messaging.sendEmail( new List<Messaging.Email>{em} );

Let me know the results.
 
Ravikiran N CRavikiran N C
@JJames , are you able to finish this requirement , I need some help on this , Please let us know your implementation