function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Reppin__505Reppin__505 

PLEASE HELP!!!! Visual Force Email Template Attachment

Hi there,

 

From what i am reading, it is possible to send a pdf attachment in a visual force email template. But only if that attachment is created on the fly using merge fields. 

 

Is it possible to attach a pdf file that is stored in the Notes & Attachments object, and have that attachment sent with the visual force email template?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sherodsherod

You need to write custom code to do this.

 

//Your query to get the body...
Blob body = [select body from Attachment limit 1].body;

//Sets up the new attachment, invents a filename
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
		efa.setFileName('attachment.pdf');
		efa.setBody(body );

//Constructs email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
String addresses = 'youremail@you.com';
String[] toAddresses = addresses.split(':', 0);
email.setSubject( 'hello');
email.setPlainTextBody( 'hello');
		email.setToAddresses(toAddresses );
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SendEmailResult [] r = 
			Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   

 

All Answers

sherodsherod

You need to write custom code to do this.

 

//Your query to get the body...
Blob body = [select body from Attachment limit 1].body;

//Sets up the new attachment, invents a filename
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
		efa.setFileName('attachment.pdf');
		efa.setBody(body );

//Constructs email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
String addresses = 'youremail@you.com';
String[] toAddresses = addresses.split(':', 0);
email.setSubject( 'hello');
email.setPlainTextBody( 'hello');
		email.setToAddresses(toAddresses );
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SendEmailResult [] r = 
			Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   

 

This was selected as the best answer
Reppin__505Reppin__505

Thank you Sherod, 

 

I had come accross that code in the documentation. But i kind of dismissed it because it is essentailly another direction completely. The reason i need to use an email template is because it is involved in a workflow rule.

 

If deploying an email with an email attachment is only possible with an apex coded email, then i guess that is what i will have to do. But before embarking on that journey and scrapping my existing visual force email template, i wanted to make sure that i absolutely have to.

 

It is just really frustrating and now i have to tell the client...

Reppin__505Reppin__505

Thanks for your help Sherod. I literally was able to copy and paste your code to start testing.

dfpittdfpitt

So, is this NOT possible from a VF email template? I also wanted to do this and leverage a workflow rule.

AdamZAdamZ

Hi Reppin.  What did you end up doing here?  I'm in a simliar situation...

 

Thanks!

LalitLalit

Please let me know if you find a way to attach files from "Notes and Attachment" with Visual-force template. With Approval e-mail request(which is a visual-force email template) approval like to see all the attachment(2-3 generally) supporting approval request.  Let me know how is it possible to attach them from  Notes and Attachment

mat_tone_84mat_tone_84
after 1 year since last post noone solve the problem.... 
I have the some problem, I need to create a template with attachment saved in "notes and attachment" of opportunity
Marianne KomeskiMarianne Komeski
Hello - we are also in need of functionality that allows for approval email alerts to contain documents that are attached to record sent for approval in Notes & Attachments section.  Further, we have multi-stage approval processes and need these emails to trigger to next approver in line upon approval by current stage approver (ie:  All approvers do not get email with documents at same time). 
Brian ZardusBrian Zardus
Did anyone find a visualforce workaround for this? Thank you!