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
KeithJKeithJ 

Cannot Send Email when including VFEmail Template

Hi there.
I have the following apex class that is to send an email which includes
a visualforce email template.
Unfortunately when I include setTemplateID method that
has a parameter of the exact VF email template I want to send,
I get false. Everytime. If I remove the setTemplateID, I get an email,
minus my template.
Can anyone help me out please?
Here is my apex class
 
public class EmailCardListController {
 
public Boolean emailSent { get; set; }
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'jbloggs@salesforce.com'};
 
    public PageReference sendMail() {
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('jbloggs@salesforce.com');
        mail.setSenderDisplayName('Salesforce Support');
        mail.setSubject('New Case Created : ' + case.Id);
        mail.setUseSignature(false);
        mail.setTemplateId('00X200000015XFL');
        try {
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   
            emailSent = true;
            return null;
        }catch(Exception e) {
             System.debug(e.getMessage());
             emailSent = false;
             return null;
        }
    }
}
 
And here is my Template;
<messaging:emailTemplate subject="Christmas Card List" recipientType="User" relatedToType="AccountTag" rendered="true">
    <messaging:htmlEmailBody >
     <p> please work! </p>
    </messaging:htmlEmailBody>
   <messaging:attachment fileName="cardList.csv">
         <c:cardList />
   </messaging:attachment>
</messaging:emailTemplate>
 
The custom component cardList is:
<apex:component controller="ChristmasCardController" access="global">
    <apex:repeat var="taggedAccount" value="{!taggedAccountsInformation}" >
        {!taggedAccount.Name}, {!taggedAccount.BillingStreet}, {!taggedAccount.BillingCity}, {!taggedAccount.BillingPostalCode}, {!taggedAccount.BillingState}, {!taggedAccount.BillingCountry}
    </apex:repeat>
</apex:component>
 
I know the custom component works, because I have tested it within a visual force page.
Please, Please can you help? Thank you very much
Best Answer chosen by Admin (Salesforce Developers) 
KeithJKeithJ
Hi there, I was missing the following 2 lines of code:
Code:
mail.setTargetObjectId(thisUser.id);
mail.setSaveAsActivity(false);

 
Worked for me in the end, but it was very painful! :)

All Answers

KeithJKeithJ

Also, I should mention that even when I create a blank VF template

and add the associated ID to the setTemplateID it still will not work.

It will work if I remove the setTemplateID and replace it with mail.setPlainTextBody('This is the text body: ');

 

Thanks

HobbeleHobbele
I have the same Problem.

If i choose the Template, the Merge Fields are not filled.

relatedToType="Lead" in the Template

and

mail.setTargetObjectId(getLeadId()); in the Code.


It works without filling the merge fields with the Lead details.

Can anyone help us? It is very important for me!


Message Edited by Hobbele on 12-11-2008 05:22 AM
Hobbele12354624Hobbele12354624
No One have an Solution for this issue ???

Please, its very important for me!
KeithJKeithJ
Hi there, I was missing the following 2 lines of code:
Code:
mail.setTargetObjectId(thisUser.id);
mail.setSaveAsActivity(false);

 
Worked for me in the end, but it was very painful! :)
This was selected as the best answer
JPSeaburyJPSeabury
This code sample was helpful for me tonight -- thanks for sharing (and thanks for coming back to post what ended up working for you!)
KeithJKeithJ
Glad to help!