[SalesForce] Add Attachment from Notes and Attachment related list in a VF Email template

I have a requirement and I appreciate your help on this.

We have a custom object, where users create records and upload spreadsheets in "Notes & Attachment" related list.

Our requirement is, we wanted to create a VF email template that adds the attachment (xls) of the record. This VF email template will be used in our standard Approval Process.

How do i go about?

Regards,
Satish

Best Answer

Messaging:attachment documentation should help you to form the attachment by yourself rather than asking user to fill in excel if data is derived from salesforce objects and fields

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_messaging_attachment.htm

https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_messaging_emailTemplate.htm

The code mentioned is as below

<messaging:emailTemplate recipientType="Contact"

relatedToType="Account"

subject="Case report for Account: {!relatedTo.name}"

replyTo="support@acme.com">

<messaging:htmlEmailBody>
<html>
    <body>
    <p>Dear {!recipient.name},</p>
    <p>Attached is a list of cases related to {!relatedTo.name}.</p>
    <center>
    <apex:outputLink value="http://www.salesforce.com">
        For more detailed information login to Salesforce.com
    </apex:outputLink>
    </center>
    </body>
</html>
</messaging:htmlEmailBody>

<messaging:attachment renderAs="PDF" filename="yourCases.pdf">
<html>
    <body>
    <p>You can display your {!relatedTo.name} cases as a PDF</p>
    <table border="0" >
    <tr>
        <th>Case Number</th><th>Origin</th>
        <th>Creator Email</th><th>Status</th>
    </tr>
    <apex:repeat var="cx" value="{!relatedTo.Cases}">
    <tr>
        <td><a href = 
             "https://na1.salesforce.com/{!cx.id}">{!cx.CaseNumber}
            </a></td>
        <td>{!cx.Origin}</td>
        <td>{!cx.Contact.email}</td>
        <td>{!cx.Status}</td>
    </tr>
    </apex:repeat> 
    </table>
    </body>
</html>
</messaging:attachment>

Lets say you want exact attachment that user has modified ,then consider sending as link

here is some example for same

<messaging:emailTemplate subject="hai" recipientType="Contact" relatedToType="Case">
 <messaging:htmlEmailBody >
<p>Attachment to download link</p>

<apex:repeat value="{!relatedTo.Attachments}" var="a">
<p><a href="{!URLFOR($Action.Attachment.Download, a)}">{!a.Name}</a> ({!a.BodyLength} B)</p>
</apex:repeat>