0

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 followed instruction on this page Adding Attachments

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. What am I doing wrong?

<messaging:emailTemplate subject="Return Authorisation #: {!relatedTo.OrderNumber}" recipientType="Contact" replyTo="sales@xx.co.nz" relatedToType="Order">
<messaging:htmlEmailBody >
    <html>
        <style>
            body { 
            font-family: Verdana, Tahoma, sans-serif}

            table, th, td {
            border-collapse: collapse;
            }
        </style>        
        <body>          
            <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1" style="font-size:10px; text-align:right">
                <tr>
                    <td>
                        <center><img src='https://c.cs5.content.force.com/servlet/servlet.ImageServer?id=015O0000000n8WP&oid=00DO0000000Vxxx&lastMod=1465879820000' title="logo" /></center>
                        <br/><br/>
                        <apex:outputText value="Date: {0,date,dd' 'MMMM', 'yyyy}" styleClass="tright">
                            <apex:param value="{!TODAY()}" />
                        </apex:outputText><br/>
                    </td>
                </tr>
            </table>
            <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table2">
                <tr>
                    <td>
                        <b>Return Authorisation #: {!relatedTo.OrderNumber}</b>
                        <br/><br/>
                        Attached is your RMA (Return Maintenance Authority) return form as requested. Please print it and attach it to the parcel.
                        <br/><br/>
                        <strong>THE RMA NUMBER ABOVE MUST BE CLEARLY MARKED ON THE OUTSIDE OF THE PARCEL BEING RETURNED</strong>
                        <br/><br/>
                        Please ensure that the goods are sufficiently packaged to ensure that no damage occurs during transit.<br/><br/>
                        If you have any queries or, want to follow up on the collection relating to this return, please contact
                        xx on <strong>0800 xx</strong> or email <strong><a href="mailto:sales@xx.co.nz">sales@xx.co.nz</a></strong>.
                    </td>
                </tr>
                <hr/>
            </table>
            <!--Addresses -->
            <hr/>
            <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table3">
                <tr>    
                    <td width="48%">
                        <b>Customer Details:</b><br/>
                        <b>{!relatedTo.Account.Name}</b><br/>
                        {!relatedTo.Account.BillingStreet}<br/>
                        {!relatedTo.Account.BillingPostalCode} {!relatedTo.Account.BillingCity}
                        <br/><br/>
                        <b>Account Number: </b>{!relatedTo.Account.AccountNumber}<br/>
                    </td>
                    <td width="2%">&nbsp;</td>
                    <td>
                        <b>Delivery Address for Return:</b><br/>
                        {!$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="table4" style="font-size:10px">
                <tr>
                    <td>
                        <b>Customer Contact name:</b> {!relatedTo.CustomerAuthorizedBy.Name}<br/>
                        <b>Contact email:</b> {!relatedTo.CustomerAuthorizedBy.Email}<br/>
                        <b>Contact phone:</b> {!relatedTo.CustomerAuthorizedBy.Phone}<br/><br/>
                    </td>
                <tr>
                </tr>    
                    <td valign="top">
                        <b>Created by:</b> {!relatedTo.CreatedBy.Name}<br/>
                        <b>Source Order :</b> {!relatedTo.RMA_Source_Order__r.OrderNumber}<br/>
                        <b>Source Invoice:</b> {!relatedTo.RMA_Source_Invoice__r.Name}<br/>
                        <b>Case:</b> {!relatedTo.RMA_Source_Document__c}
                    </td>
                </tr>    
            </table>
            <br/>
            <hr/>
            <table border="0" width="100%" id="table5" style="font-size:10px">
                <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>
                <tr>
                    <apex:repeat value="{!relatedTo.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>  
                </tr>
            </table>
            <br/>
            <hr/>
            <br/>
            <p>{!relatedTo.Description}</p>
            <br/>
        </body>
    </html>
</messaging:htmlEmailBody>

<!-- ----------------------------------------------------------------------------------------------------------------------- -->        
<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">               
    <html>
        <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>        
        <body>
            <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 #: {!relatedTo.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>{!relatedTo.Account.Name}</h3>
                        {!relatedTo.Account.BillingStreet}<br/>
                        {!relatedTo.Account.BillingPostalCode} {!relatedTo.Account.BillingCity}
                        <br/><br/>
                        <b>Account Number: </b>{!relatedTo.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> {!relatedTo.CustomerAuthorizedBy.Name}<br/>
                        <b>Contact email:</b> {!relatedTo.CustomerAuthorizedBy.Email}<br/>
                        <b>Contact phone:</b> {!relatedTo.CustomerAuthorizedBy.Phone}
                    </td>
                    <td>
                        <b>Created by:</b> {!relatedTo.CreatedBy.Name}<br/>
                        <b>Source Order :</b> {!relatedTo.RMA_Source_Order__r.OrderNumber}<br/>
                        <b>Source Invoice:</b> {!relatedTo.RMA_Source_Invoice__r.Name}<br/>
                        <b>Case:</b> {!relatedTo.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="{!relatedTo.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>{!relatedTo.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 xxx</b> or <b>email sales@xx.co.nz</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>
        </body>
    </html>
</messaging:attachment>      
</messaging:emailTemplate>

My custom button URL is

/_ui/core/email/author/EmailAuthor?p2_lkid=001O000000NYJbV&rtype=003&p3_lkid={!Order.Id}&retURL=%2F{!Order.Id}&template_id=00XO0000000Ek5M
  • I changed the custom button Window Position = to 'Full Screen' (it was on 'No Preference') and lo and behold the attachment now DOES appear in the email! Huh? This makes no sense to me (but I'm glad the attachment is now there). – Irene Feb 13 '17 at 23:28
  • You can put that as your ans so other people can find that useful – Santanu Boral Feb 14 '17 at 3:33

Your Answer

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

Browse other questions tagged or ask your own question.