+ Start a Discussion
Kevin042Kevin042 

How can I attach files from the Notes & Attachements Related list to a VF email template?

How can I attach a file to a visualforce email template? 

 

Use Case – we are sending out a visualforce email from the Custom_Object_1__c object and listing info from the related list Child_Object__c.  Now I want to attach to the email any and all files that are listed in the Notes & Attachment related list for Custom_Object_1__c.

 

Thanks,

Kevin

kyle.tkyle.t

You will need to query the attachments table for all attachments whose parentId = your custom object IDs.  The code is from something I have in development right now and is bulkified.

 

List<Attachment> atchmnts = new List<Attachment>(); 
		atchmnts = [Select a.ParentId, a.IsPrivate, a.Name, a.IsDeleted, a.Body, a.ContentType, a.Id From Attachment a where a.ParentId IN :trigger.new];

 

 

Once you have the attachments, you can create a maping of the IDs to Sets of attachments so that you can create the emails for each custom object and loop through the set of attachments and add all attachments to the email.  There may be a better way to add an attachment to the set, but this works for me.

 

Map <ID, Set<Attachment>> attachmentMap = new Map <ID, Set<Attachment>>();
		for(Attachment a : atchmnts){
			if(attachmentMap.containsKey(a.ParentId)){
				Set<Attachment> aSet = attachmentMap.get(a.ParentId);
				attachmentMap.remove(a.ParentId);
				aSet.add(a);
				attachmentMap.put(a.ParentId, aSet);
			}else{
				Set<Attachment> aSet = new Set<Attachment>();
				aSet.add(a);
				attachmentMap.put(a.ParentId, aSet);
			}	
		}

 For me, this is a trigger on the opportunity, so now I loop through all of the opportunities in the trigger and create the emails (o.id is the id of the opportunity in this iteration of the loop). The code below is for adding the attachments to the email.

 

 

if(attachmentMap.containsKey(o.id)){
    //Declare a List of attachments for the mail 
    List<Messaging.EmailFileAttachment> a = new List<Messaging.EmailFileAttachment>();
					
	//add all attachments to the email
	for(Attachment att : attachmentMap.get(o.id)){
		Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
		efa.setBody(att.Body);
		efa.setContentType(att.ContentType);
		efa.setFileName(att.Name);
		efa.setInline(false);
		a.add(efa);
	}	
    mail.setFileAttachments(a);
}

 

 

 

Hope that helps.  As far as Notes, I belive you can do something similar by querying the Notes table.

 

 

 

 

Kevin042Kevin042

I am still kind of new to this visualforce stuff.

 

Where in the vf email would I put this.  I am just not sure where to put it in the template or would I create a component? and put the component between the <messaging:attachment> </messaging:attachment> sections?

 

Here is what I have so far:

 

<messaging:emailTemplate subject="Renewal Rates for {!relatedTo.name} - {!relatedTo.Renewal_Date_Formula__c}" recipientType="User" relatedToType="Renewal__c">
<messaging:htmlEmailBody >
  <html>

        <body>
         <STYLE type="text/css">
               TH {font-size: 12px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: Center}
               TD  {font-size: 12px; font-face: verdana }
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
             <font face="arial" size="2">
<p>{!recipient.firstname},</p>




<p>Below are the renewal rates related to the account: <strong>{!relatedTo.name}</strong>.</p>

<p>Renewal Date: <strong>{!relatedTo.Renewal_Date_Formula__c}</strong> </p>
<p>Sub-Clients: <strong>{!relatedTo.SubClients__c}</strong> </p>
<p>Underwriter: <strong>{!relatedTo.Underwriter__r.Name}</strong> </p>



<c:Renewal_Premiums RenewalID="{!relatedTo.id}">   </c:Renewal_Premiums>

<p/>
<p/>
<a href="https://na6.salesforce.com/{!relatedTo.id}">Click here to view rates in Salesforce.</a>
<p/>
        
        
<p>To update your renewal notification settings <a href="https://na6.salesforce.com/{!relatedTo.OwnerID}">click here</a></p>

<p/>         
<p/>         
        
        
 </font>
        </body>

 

    </html>

</messaging:htmlEmailBody>
</messaging:emailTemplate>

kyle.tkyle.t

The code that I posted earlier exists in a trigger that I have written.   You could do the same in a class.  The code would reference the template (using setTemplateId) while building the email.  You will want to look in the apex documentation at the messaging class.

 

 

bdouglas1bdouglas1

Kevin, 

 

Were you able to make this solution work or did you go another route?

 

Thanks,

Bryan

LalitLalit

Please let me know if you are managed to attach files from notesandattachment section into a visual-force email template.

We have a similar requirement, with sales-form (a custom object) user inset Attachment(contains Rebate check detail or item description).
In Approval e-mail request Accounting/Finance team needs to see all the attached document under Notes&Attachment section. Appreciate any input.

bdouglas1bdouglas1

You can't do this directly through a Visualforce email template.  I created a trigger that builds the email with the Visualforce template and then adds the necessary attachments.

LalitLalit

Thanks for the update,

Can you please share your code or logic if possible,

Also how do you trigger the e-mail. since current visualforce e-mail template is part of approval process(work-flow rule).

 

Apprecaite any input.