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
alonmalonm 

send an Email using a visualforce template, with a pdf attachment

Hello,

I am trying to send an Email using a visualforce template, with a pdf attachment, but can't seem to make it work. In my application i have a "sutTer__c" object, which is linked to a "traveller" object. This is a simplified version of the Email template:

 

<messaging:emailTemplate subject="TER" recipientType="User" relatedToType="sutTer__c">
<messaging:plainTextEmailBody >
Attached the TER details
</messaging:plainTextEmailBody>
<messaging:attachment renderAs="pdf" filename="ter.pdf">
    This is the Attachment fot Ter number&nbsp;
    <apex:outputText value="{!relatedTo.name}"/>
</messaging:attachment>   
</messaging:emailTemplate>

 

This is the code that should send the Email (at this point there is an object ter of the type sutTer__c):

        

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {ter.Traveller__r.user__r.Email});
mail.setTemplateId('00X800000018i7A');
mail.setWhatId(ter.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

However, when i try to send an Email with this code, i get an error massage:

"Missing targetObjectId with template."

So i set targetObjectId (and set the saveAsActivity to false,to prevent another error massage), and only then send the mail:

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {ter.Traveller__r.user__r.Email});
mail.setTemplateId('00X800000018i7A');
mail.setWhatId(ter.id);

mail.setTargetObjectID(ter.traveller__r.user__r.id);

mail.setSaveAsActivity(false);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

in this case the email is being sent, but it is empty (no body and no attachment). Can anyone tell me what am I doing wrong?

 

thanks,

 

Alon

 

 

 

 

Message Edited by alonm on 05-06-2009 02:24 AM
Best Answer chosen by Admin (Salesforce Developers) 
rscottrscott

I am having the same issue. Blank emails being sent. It is picking up the subject from the template, so it's doing something, but no body. Anyone with any ideas?