I am working on setting up a scheduled apex class to send out emails to me every day. I have a visualforce template that takes a user and an opportunity as parameters that I would like to use. The scheduler works great and an email gets sent but the message body is blank. Here is my code that builds the email. Is there something I am doing wrong? I have tested the visualforce template by sending it to myself through the salesforce UI and it works fine.

          public static void mail60(Opportunity op)

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    string [] toaddress= New string[]{'my email address'};
    email.settemplateid(' template ID here'); //In the real code I have the correct IDs in place
    email.setSubject( 'Blah - Maintenance and Support Renewal');
    email.setTargetObjectId(' contact ID here '); //In the real code I have the correct IDs in place
    email.setWhatId(' opportunity ID here '); //In the real code I have the correct IDs in place
    email.saveAsActivity = false;
    Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});

  • 1
    You should include the code for the email, the template markup & any controllers associated with it. You said it yourself, the email sends & arrives fine, but the body is blank, so its probably not an issue with your sending code, but your email template. – battery.cord Apr 11 '18 at 20:00
  • I have tested the email template with the 'Send Test and Verify Merge Fields' button and I have tried sending it to myself (I added myself as a contact in Salesforce) from the Salesforce UI and it works in both cases. The controller and visualforce component are lengthy, if you need to see them still I will post it. – Rory Apr 11 '18 at 20:06
  • Maybe a reduced version which still behaves the same? Removing all of the markup except for a field value or two for testing? – battery.cord Apr 11 '18 at 20:08
  • Oen nitpick with your post - you say you have a template which takes a user and a opportunity, but you provide a contact id for the targetobjectId? Do you provide a user id when testing through the UI, or a contact Id? – battery.cord Apr 11 '18 at 20:09

There are few things wrong,

  1. when using template you dont have to set subject.
  2. When you use setTargetObjectId, your email is sent to that contact/lead or user. You dont have to set toAddress

Instead of such complex code, you can do it

Messaging.SingleEmailMessage email = 
            Messaging.renderStoredEmailTemplate(templateId, whoId, whatId);

So your code will be like

public static void mail60(Opportunity op){

    Messaging.SingleEmailMessage email = 
                Messaging.renderStoredEmailTemplate('templateId', ' contact ID here ', op.Id);

    string [] ccAddress= new string[]{'my email address'};
    email.saveAsActivity = false;
    Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});


SRC: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm


  • 1
    Made my code simpler and made it work. Thank you! – Rory Apr 11 '18 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.