+ Start a Discussion
Irene SlessIrene Sless 

VF email template with attachment not sending attachment

I created a VisualForce email template that includes building up an attachment. If I test it from the template it all works nicely and includes the attachment when sending the email.

I then created a custom button on my Order object detail page with a URL (have also tried Javascript) that runs this template. The email is sent but the attachment is nowhere to be found. Debug log doesn't tell me anything so I have no idea what's happening. 
<apex:page standardController="Order" showHeader="false" applyHtmlTag="false" applyBodyTag="false" renderas="pdf">
    <head>
        <style>
            body { 
            font-size:10px;
            font-family: Verdana, Tahoma, sans-serif}
            
            h2 {
            font-size:14px; 
            font-weight: bold; 
            line-height: 16px}
            
            h3 {
            font-size:12px; 
            font-weight: bold; 
            line-height: 14px }
            
            h4 {
            font-size:11px; 
            font-weight: bold; 
            line-height:13px}
            
            table, th, td {
            border-collapse: collapse;
            }
            
            th { font-weight: bold; }
                        
            @page {
                counter-increment: pages;
                @top-center {content: counter(page) " of " counter(pages);}
                @bottom-center {content: element(footer);}
            }
            div.footer {position: running(footer)}
            #pageNum: before {content: counter(pages);}
        </style>        
    </head>
    
    <div class="footer" style="font-size:8pt;text-align:center">

		<p align="center"><i>Copyright {!$Organization.Name}</i></p>
         
        <apex:outputText value="Printed: {! NOW() }"/>
        
    </div>
    <!--Logo and RMA number -->
    <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
        <tr>
            <td>
                <img src='{!URLFOR($Resource.xx_address)}' title="logo" />
            </td>
        </tr>
        <hr/>
    </table>
    <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
        <tr>
            <td>
                This number must be attached to all items being returned<br/>
            </td>
            <td align="right">
                <h2>Return Authorisation #: {!Order.OrderNumber}</h2>
            </td>
        </tr>
        <hr/>
    </table>
    
    <!--Addresses -->
    <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
        <tr>    
            <td width="48%">
                <h4>Customer Details</h4>
                <h3>{!Order.Account.Name}</h3>
                {!Order.Account.BillingStreet}<br/>
                {!Order.Account.BillingPostalCode} {!Order.Account.BillingCity}
                <br/><br/>
                <b>Account Number: </b>{!Order.Account.AccountNumber}<br/>
            </td>
            <td width="4%">&nbsp;</td>
            <td>
		        <apex:outputText value="Date: {0,date,dd' 'MMMM', 'yyyy}">
                    <apex:param value="{!TODAY()}" />
                </apex:outputText><br/>
                <h4>Delivery Address for Return</h4>
                {!$Organization.Name}<br/>
                {!$Organization.Street}<br/>
                {!$Organization.PostalCode} {!$Organization.City}<br/>
                {!$Organization.Country}<br/>
            </td> 
        </tr>
    </table>
    <br/>
    <hr/>
    
    <table border="0" width="100%" id="table2">
        <tr>
            <td>
                <b>Contact name:</b> {!Order.CustomerAuthorizedBy.Name}<br/>
                <b>Contact email:</b> {!Order.CustomerAuthorizedBy.Email}<br/>
                <b>Contact phone:</b> {!Order.CustomerAuthorizedBy.Phone}
            </td>
            <td>
                <b>Created by:</b> {!Order.CreatedBy.Name}<br/>
                <b>Source Order :</b> {!Order.RMA_Source_Order__r.OrderNumber}<br/>
                <b>Source Invoice:</b> {!Order.RMA_Source_Invoice__r.Name}<br/>
                <b>Case:</b> {!Order.RMA_Source_Document__c}
            </td>
        </tr>    
    </table>
    <br/>
    <hr/>
    <table border="0" width="100%" id="table3">
        <tr>
            <td bgcolor="#C0C0C0"><h4>Product</h4></td>
            <td bgcolor="#C0C0C0"><h4>Description</h4></td>
            <td bgcolor="#C0C0C0"><h4>Quantity</h4></td>
            <td bgcolor="#C0C0C0"><h4>Unit</h4></td>
        </tr>
        <apex:repeat value="{!Order.OrderItems}" var="line">
            <tr>
                <td>{!line.PricebookEntry.ProductCode}</td>
                <td>{!line.Description}</td>
                <td>{!line.Quantity}</td>
                <td>{!line.Unit__c}</td>
            </tr>
        </apex:repeat>  
    </table>
    <br/>
    <hr/>
    <br/>
    <p>{!Order.Description}</p>
    <br/>
    <hr/>
    <table width="100%" id="table5">
        <tr>
            <td><b>This is your RMA (Return Maintenance Authority) return form as requested.<br/>
                THE RMA NUMBER ABOVE MUST BE CLEARLY MARKED ON THE OUTSIDE OF THE PARCEL BEING RETURNED</b>
                <br/>
                Please ensure that the goods are sufficiently packaged to ensure that no damage occurs during transit.<br/>
                If you have any queries or, want to follow up on the collection relating to this return, please contact
                xx on <b>0800 xxxxx</b> or <b>email sales@xx.com</b>
            </td>
        </tr>
        <tr>
            <td alight="center">
                <h3>Return Reason - please complete</h3>
			    <table border="1" width="95%" height="100px">
                    <tr><td height="100px"></td></tr>
                </table>
            </td>
        </tr>
    </table>
</apex:page>

Button URL is as below, the template id taken from the above email template.
/_ui/core/email/author/EmailAuthor?p2_lkid=001O000000NYJbV&rtype=003&p3_lkid={!Order.Id}&retURL=%2F{!Order.Id}&template_id=00XO0000000Ek5M
DixitDixit
I have never used VF templates, but the documentation is not looking as your code.
Can you please check this links?:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_creating.htm

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_attachments.htm
Irene SlessIrene Sless
Hi Dixit, I tried to delete this post (as I hadn't copied the complete template - the attachment part was missing), but I see the delete didn't work - it's still here! Either way, what I've discovered since that if I set my button's Window Position to 'Full Screen' (it was on 'No Preference' before), the attachment now does get attached to the email. It makes no sense to me, but hey, I'm happy it's there!