+ Start a Discussion
Glenn at MvistaGlenn at Mvista 

Getting attachments as well as !relatedTo info into a template

I have a Vforce email template that will correctly pull things like the Opportunity Name and the Closed Date from an Opportunity with typical fields like {!relatedTo.Name} or {!relatedTo.StageName}.


I wanted to add any records in Notes and Attachments to the email as well and found out how to do that with a trigger, as shown:



trigger sendEmailAlert on Opportunity (after update) {

for(opportunity o:Trigger.new){
if(o.Stagename == 'Order Credit Hold'){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

Attachment[] queryAttachment = [select Name, Body from Attachment where Parentid=:o.Id order by CreatedDate DESC];

Messaging.EmailFileAttachment[] allAttachments = new Messaging.EmailFileAttachment[queryAttachment.size()];
        for(integer i=0;i<queryAttachment.size();i++)
            Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
            allAttachments[i] =  fileAttachment;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });




Which works to add the attachments to the email and the layout of the template is correct.


However, the !relatedTo fields are not getting pulled and used.  Any one know how to put the two things together?