+ Start a Discussion
Chrystele BenjaminChrystele Benjamin 

Opportunity's Notes & Attachments into email template

Hello,
Could anyone help me fix the bold portion of this visualforce email template code so that any document attached to the opportunity in the Notes & Attachments section gets included in the email alert as an attachment?

<messaging:emailTemplate recipientType="Contact"
    relatedToType="Opportunity"
    subject="CSF processing request for: {!relatedTo.name}"
    replyTo="xxxx.xxxx@xxx.net" >
   
<messaging:htmlEmailBody >       
    <html>
        <body>
         <STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center }
               TD  {font-size: 11px; font-face: verdana }
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
        <p>CSF PROCESSING REQUEST</p>
        <p>Please find attached the detailed CSF for:<p/>
        <p> Opportunity:<b> {!relatedTo.name}</b>
        <br/>Account: <i><b> {!relatedTo.Account.name}</b>  </i>         
        <br/>Opp ID:<b> {!relatedTo.Opp_ID__c} </b>
        <br/>Opportunity Owner: <b>{!relatedTo.owner.name}</b>
        <br/>Operating Region: <b> {!relatedTo.xxxxxx_Operating_Region2__c}</b>
        <p/>       
       <p />
 </font>
        </body>
    </html>
</messaging:htmlEmailBody>       
       
        <messaging:emailTemplate subject="Opps Signed Documents" recipientType="Contact" relatedToType="Opportunity">
    <messaging:htmlEmailBody >
        <p>Attachments</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>
    </messaging:htmlEmailBody>
Best Answer chosen by Chrystele Benjamin
Abhishek BansalAbhishek Bansal
If posible can you please recah out to me direclty so that we can connect and finish this off.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102

All Answers

Abhishek BansalAbhishek Bansal
Hi Chrystele,

You need to use the <messaging:attachment> tag to add the attachments in email template. Sample code is given below:
<messaging:attachment>
		<apex:repeat var="cx" value="{!relatedTo.Cases}">
			Case Number: {!cx.CaseNumber}
			Origin: {!cx.Origin}
			Creator Email: {!cx.Contact.email}
			Case Number: {!cx.Status}
		</apex:repeat> 
	</messaging:attachment>

You can get more help on this link:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_attachments.htm 

Let me know if you face any more issues.

Thanks,
Abhishek Bansal.
Chrystele BenjaminChrystele Benjamin
Thanks Abhishek,
I've seen that example before but I don't know how to make it work to pull out of the opportunity object. I get error messages. I tried replacing "cases" with "Opportunity" and the next 4 lines with opp fields but I'm very elementary still with coding so what do I use instead of "cx"? Any chance you can update my portion of the code? Pretty please?? :-)
Chrystele BenjaminChrystele Benjamin
Also, which portion of your sample code points to the documents in the opp's Notes & Attachments section?
Abhishek BansalAbhishek Bansal
You can replace relatedTo.Cases with relatedTo.Attachments
You can use the code that you are using but instead of <messaging:emailTemplate> tag you should use <messaging:attachment>
Chrystele BenjaminChrystele Benjamin
I think I'm going to give up on this. I asked in so many places and no one is willing to fix it for me. I tried what you just said and I'm getting this error (Error: XML document structures must start and end within the same entity at line 41) Line 41 = Creator Email: {!cx.Contact.email}
Here is the full code with your example in bold:
<messaging:emailTemplate recipientType="Contact"
    relatedToType="Opportunity"
    subject="CSF processing request for: {!relatedTo.name}"
    replyTo="xxx.xxx@xxx.net" >
   
<messaging:htmlEmailBody >       
    <html>
        <body>
         <STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center }
               TD  {font-size: 11px; font-face: verdana }
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
        <p>CSF PROCESSING REQUEST</p>
        <p>Please find attached the detailed CSF for:<p/>
        <p> Opportunity:<b> {!relatedTo.name}</b>
        <br/>Account: <i><b> {!relatedTo.Account.name}</b>  </i>         
        <br/>Opp ID:<b> {!relatedTo.Opp_ID__c} </b>
        <br/>Opportunity Owner: <b>{!relatedTo.owner.name}</b>
        <br/>Operating Region: <b> {!relatedTo.xxxxx_Operating_Region2__c}</b>
        <p/>                                       
       <p />
 </font>
        </body>
    </html>   
        <messaging:attachment>
        <apex:repeat var="cx" value="{!relatedTo.Attachments}">
            Opportunity: {!cx.Name}
            Account: {!cx.AccountID}
            Creator Email: {!cx.Contact.email}
            Opp ID: {!cx.Opp_ID__c}
        </apex:repeat>
    </messaging:attachment>
Abhishek BansalAbhishek Bansal
If posible can you please recah out to me direclty so that we can connect and finish this off.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102
This was selected as the best answer
Chrystele BenjaminChrystele Benjamin
Ok awesome! I sent you an email. Thanks so much!!
Chrystele BenjaminChrystele Benjamin
Thanks so much Abhishek! I am now all set and my opps' attachments get included in my email alerts. You are a Star!!
I will highly recommend you every chance I get.
Thank you,
CB