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
NoodleNoodle 

Visualforce Template, Scheduled Class RelatedToType

Hi there. I have a Visualforce template that I want to use to send out a weekly email.
 
<messaging:emailTemplate subject="Your late Tasks" recipientType="User" 

    relatedToType="User"
    replyTo="cases@acme.nomail.com" >

 <messaging:htmlEmailBody >
 
  .............
.............
.............
..........

 </messaging:htmlEmailBody>

</messaging:emailTemplate>

Then I have my Scheduled Class:
global class massTaskEmail implements Schedulable {
      
    global void execute(SchedulableContext sc) {
   
      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
      
        email.setTemplateId('templateId');
	    email.setSaveAsActivity(false);
	    email.setTargetObjectId('userId');
            
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});   
     }
}

I would like to test this out by inserting both UserID's like I would if I was trying to preview the template. I already have the Object Target via setTargetObjectId('userId'); . How do I set the second one?

User-added image
SonamSonam (Salesforce Developers) 
In your template, I see that the  recipientType and relatedToType are both User object so you can test only by setting setTargetObjectId('userId'); 
setTargetObjectId(ID) will ensure the User merge fields get the correct value.

If your case was as below:
recipientType = Contact and relatedToType= Account
Then you would use setWhatId(ID)(optional) to get the merge fields related to the account object on the contact.

reference:
http://www.salesforce.com/docs/developer/pages/Content/pages_compref_messaging_emailTemplate.htm​
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm#apex_Messaging_SingleEmailMessage_constructors