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
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12 

Pass Id parameter to VF Email Component

I have a email that is being sent out by a scheduled batch class that I need to pass a Recordc Id paramerter to.  I am using a VF email template with a component and class that generates a table of activities based on information related to the recipients user record.  The batch and email work fine but in order to get the components table to populate correctly I need to pass the user ID to the VF Components controller, and cannot figure out how to do that.  Bleow is a snippet from my controller and accomodating classes.

Controller:

public with sharing class RACnSACEmailClass {

    public List<User> userRecord = new List<User>();
    public Id userId {get; set;}
    public list<Task> taskList {get; set;}
    public list<Event> eventList{get; set;}
    public string emailAdd {get; set;}
    public string region {get; set;}
    public list<activityWrapper> actList {get; set;}
    public list<activityWrapperImp> actListImp {get; set;}
    public string firm;
    public string name;
    public id cId;
    public string emailHeader {get; set;}
    public datetime todaysDate = system.now();
    public string weekDay {get; set;}
    public date queryRange {get; set;}
    Public Id setuserId(ID a){
         userId = a;
         return userId;

         }
    public RACnSACEmailClass() {
        actList = new list<activityWrapper>();
        actListImp = new list<activityWrapperImp>();

        userRecord = [select Id, Region__c from User Where Id = :userId];
        region = userRecord[0].Region__c;
        weekDay = todaysDate.format('EEEE');
        system.debug('***** the date is '+weekDay);
        IF(weekDay != 'Monday'){
            queryRange = todaysDate.Date().addDays(-1) ;
        }else{
            queryRange = todaysDate.Date().addDays(-3);
        }
        taskList = [SELECT Id, Region__c, Description, Priority, Activity_Type__c, Meeting_Type__c, Subject, CreatedBy.FirstName,
                    CreatedBy.LastName, ActivityDate,  Activity_Status__c, Owner.Name,     CreatedDate
                    FROM task
                    WHERE CreatedDate >= :queryRange AND  Region__c = :region AND Activity_Type__c != 'Email'] ;
        system.debug('***** there are '+taskList.size()+' tasks.');
        IF(taskList.size() > 0){

Component

<apex:component access="global" controller="RACnSACEmailClass">
<apex:attribute name="recordId" description="Record Id" type="id" assignTo="{!userId}"/> 
<body>
  <p>
  <apex:outputtext value="{!emailHeader}" style="font-weight:bold; font-size: 14px"/>
</p>
  <table class = "deviceWidth" cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
    <tr>
      <th bgcolor="#E67300" style="font-size:14; font-family:Sans-SerIF; font-weight:bold;text-align:center;width:20%;white-space:nowrap;">
      <font color="#FFFFFE">High Importance Activities</font>
      </th>
    </tr>
    <apex:repeat value="{!actListImp}" var="a">
    <tr valign="center" bgcolor="#DDDDDD" style="font-size:12;font-family:Sans-SerIF;font-weight:bold; width:100%">
      <td>
        <apex:outputLink value="/{!a.contactId}" id="theContactLink">{!a.ContactName}</apex:outputLink><apex:outputText Value=" - {!a.actFirm}"/>
      </td>
    </tr>
    <tr style="font-size:12; font-family:Sans-SerIF; line-height:125%;">
      <td>
        <Div>Activity Date: {!a.actDate}
        <br/>
        <apex:outputLink value="/{!a.actId}" id="theActivityLink">{!a.actSubject}</apex:outputLink><apex:outputText Value=" - {!a.actSubType}"/>
        <br/>Logged By: {!a.actOwnerName}
        <br/>
        <b> Desc:</b>{!a.actDescription}
        </Div>
      </td>
    </tr>
    </apex:repeat>
  </table>
  <br/>
  <table cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
    <tr>
      <th bgcolor="#C8C88C"  style="font-size:14;font-family:Sans-SerIF;font-weight:bold;text-align:center;width:20%;white-space:nowrap;">
      <font color="#FFFFFE">Other Activities</font>
    </th>
    </tr>
    <apex:repeat value="{!actList}" var="a">
    <tr valign="center" bgcolor="#DDDDDD" style="font-size:12;font-family:Sans-SerIF;font-weight:bold; width:100%">
      <td>
        <apex:outputLink value="/{!a.contactId}" id="theContactLink">{!a.ContactName}</apex:outputLink><apex:outputText Value=" - {!a.actFirm}"/>
      </td>
    </tr>
    <tr style="font-size:12; font-family:Sans-SerIF; line-height:125%;">
      <td>
        <Div>Activity Date: {!a.actDate}
        <br/>
        <apex:outputLink value="/{!a.actId}" id="theActivityLink">{!a.actSubject}</apex:outputLink><apex:outputText Value=" - {!a.actSubType}"/>
        <br/>Logged By: {!a.actOwnerName}
        <br/>
        <b> Desc:</b>{!a.actDescription}
        </Div>
      </td>
    </tr>
    </apex:repeat>
  </table>
</body>

</apex:component>

VF Email

<messaging:emailTemplate subject="RAC and SAC Daily Digest [ EXTERNAL ]" recipientType="User" relatedToType="User">
  <messaging:htmlEmailBody >
    <c:RACnSACDigestEmailComponent recordId="{!RelatedTo.ID}" />
  </messaging:htmlEmailBody>
</messaging:emailTemplate>

 

Batch class

global class RACnSACDigestSCRevisionBatch implements Database.Batchable<sObject> {
    
    
    
    String userRegions = 'select id, Region__c, Email from user where Region__c != Null Order by Region__c asc';
    
    global RACnSACDigestSCRevisionBatch() {
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(userRegions);
    }
    
    global void execute(Database.BatchableContext BC, List<User> scope) {
        List<Messaging.SingleEmailMessage> emailList = new list<Messaging.SingleEmailMessage>();
        List<emailTemplate> emailTemplateList =[select id from emailTemplate Where name = 'RACnSACDigestEmail'];
        string templateId = emailTemplateList[0].Id;
        List<string> emailAddress = new list<string>();
        emailAddress.add('aaldis@ofiglobal.com');
        system.debug('***** scope size is '+scope.size());
        for(User u: scope){
            Id userID = u.Id;
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(emailAddress);
            email.setTargetObjectId(userId);
            email.setWhatId(userId);
            email.setTemplateId(templateId);
            email.setSaveAsActivity(false);
            email.setTreatTargetObjectAsRecipient(false);
            system.debug('***** scope size is '+email);
            emailList.add(email);
            //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        }
        system.debug('***** email list size is '+emailList.size());
        Messaging.sendEmail(emailList);
        
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
    
    
}