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 

User a VF email component with a batch class

I am trying to figure out how to access the TargetObjectId of a batch email to use in the Email Template VF Component.  I set the target Id in my batch and tried assigning it as an Attribute in my template and component but it does not seem to access the record I in my controller.  How do I access that value in my controller?

Here is my component:
<apex:component access="global" controller="RACnSACEmailClass">
<apex:attribute name="recordId" description="Record Id" type="id" assignTo="{!userId}"/>

</apex:component>

Email Template

<messaging:emailTemplate subject="" recipientType="User" relatedToType="User">
  <messaging:htmlEmailBody >
     <c:RACnSACDigestEmailComponent recordId="{!recipient.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 and isActive = True 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){

            System.debug('***** user id is '+u.Id+' region in '+u.region__c+' email is '+u.email);
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            //email.setToAddresses(emailAddress);
            email.setTargetObjectId(u.Id);
            email.setWhatId(u.Id);
            email.setTemplateId(templateId);
            email.setSaveAsActivity(false);
            email.setCcAddresses(emailAddress);
            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) {

    }


}
 and Controller:
global with sharing class RACnSACEmailClass {

    public User userRecord {get; set;}
    public Id userId {get; set;}
    public Id recordId {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 getUserId(){
      return userId;
    }

    public string getUserRecord(string user){
      userId = user;
      userRecord = [select Id, Region__c from User Where Id = :userId];
      if(userRecord != Null){
        region = userRecord.Region__c;
      }
      return region;
    }

    public RACnSACEmailClass() {

        actList = new list<activityWrapper>();
        actListImp = new list<activityWrapperImp>();



        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' limit 3] ;
        system.debug('***** there are '+taskList.size()+' tasks.');
        IF(taskList.size() > 0){
            FOR(integer i = 0; i < taskList.size(); i++){
                List<taskRelation> tr = [select relationId from taskRelation where taskId = :taskList[i].Id Order By CreatedDate asc limit 1];
                IF(tr.size() > 0){
                    List<contact> c = [select id, Name, Account.Name from contact where Id = :tr[0].relationId];
                    firm = c[0].Account.Name;
                    name = c[0].Name;
                    cId = c[0].Id;
                }
                If(taskList[i].Priority != 'High'){
                    actList.add(new activityWrapper(taskList[i], firm, name, cId));
                }else{
                    actListImp.add(new activityWrapperImp(taskList[i], firm, name, cId));
                }
            }
            firm = null;
            name = null;
            cId = null;
        }

        eventList = [SELECT Id, Region__c, Description, StartDateTime, Activity_Priority__c, Activity_Type__c, Meeting_Type__c, Subject,
                     CreatedBy.LastName, CreatedBy.firstName, Activity_Status__c, Owner.Name, CreatedDate
                     FROM event
                     WHERE /*CreatedDate >= :queryRange AND */ Region__c = :region  AND Activity_Type__c != 'Email' limit 3] ;
        IF(eventList.size() > 0){
            FOR(integer i = 0; i < eventList.size(); i++){
                List<eventRelation> ev = [select relationId from eventRelation where eventId = :eventList[i].Id Order By CreatedDate asc limit 1];
                IF(ev.size() == 1){
                    List<contact> c = [select id, Name, Account.Name from contact where Id = :ev[0].relationId];
                    firm = c[0].Account.Name;
                    name = c[0].Name;
                    cId = c[0].Id;
                }
                If(eventList[i].Activity_Priority__c != 'High'){
                    actList.add(new activityWrapper(eventList[i], firm, name, cId));
                }else{
                    actListImp.add(new activityWrapperImp(eventList[i], firm, name, cId));
                }
            }
            firm = null;
            name = null;
            cId = null;
        }
        IF(eventList.size() == 0 && taskList.size() == 0){
            EmailHeader ='No activities were logged yesterday in region - '+region;
        }else{
            EmailHeader ='All activities logged yesterday in region - '+region;
        }
    }

    public class activityWrapper{
        public string type{get; set;}
        public string actId {get; set;}
        public string actType {get; set;}
        public string actSubType {get; set;}
        public string actSubject {get; set;}
        public DateTime actDate {get; set;}
        public DateTime actCreatedDate {get; set;}
        public string actOwnerName {get; set;}
        public string actDescription {get; set;}
        public string actStatus {get; set;}
        public string actFirm {get; set;}
        public string contactName{get; set;}
        public Id contactId {get; set;}
        public boolean important = false;

        public activityWrapper(task tsk, string firm, string name, id cId){
            actId = tsk.Id;
            actType = tsk.Activity_Type__c;
            actSubType  = tsk.Meeting_Type__c;
            actSubject = tsk.Subject;
            actDate = tsk.ActivityDate;
            actCreatedDate  = tsk.CreatedDate;
            actOwnerName = tsk.Owner.Name;
            actDescription  = tsk.Description;
            actStatus = tsk.Activity_Status__c;
            contactName = name;
            actFirm = firm;
            contactId = cId;
        }

        public activityWrapper(event evt, string firm, string name, id cID){
            actId = evt.Id;
            actType = evt.Activity_Type__c;
            actSubType  = evt.Meeting_Type__c;
            actSubject = evt.Subject;
            actDate = evt.StartDateTime;
            actCreatedDate  = evt.CreatedDate;
            actOwnerName = evt.Owner.Name;
            actDescription  = evt.Description;
            actStatus = evt.Activity_Status__c;
            contactName = name;
            actFirm = firm;
            contactId = cId;
        }
    }

    public class activityWrapperImp{
        public string type{get; set;}
        public string actId {get; set;}
        public string actType {get; set;}
        public  string actSubType {get; set;}
        public  string actSubject {get; set;}
        public  DateTime actDate {get; set;}
        public  DateTime actCreatedDate {get; set;}
        public  string actOwnerName {get; set;}
        public  string actDescription {get; set;}
        public  string actStatus {get; set;}
        public string actFirm {get; set;}
        public string contactName{get; set;}
        public Id contactId {get; set;}

        public activityWrapperImp(task tsk, string firm, string name, id cId){
            actId = tsk.Id;
            actType = tsk.Activity_Type__c;
            actSubType  = tsk.Meeting_Type__c;
            actSubject = tsk.Subject;
            actDate = tsk.ActivityDate;
            actCreatedDate  = tsk.CreatedDate;
            actOwnerName = tsk.Owner.Name;
            actDescription  = tsk.Description;
            actStatus = tsk.Activity_Status__c;
            contactName = name;
            actFirm = firm;
            contactId = cId;
        }

        public activityWrapperImp(event evt, string firm, string name, id cID){
            actId = evt.Id;
            actType = evt.Activity_Type__c;
            actSubType  = evt.Meeting_Type__c;
            actSubject = evt.Subject;
            actDate = evt.ActivityDate;
            actCreatedDate  = evt.CreatedDate;
            actOwnerName = evt.Owner.Name;
            actDescription  = evt.Description;
            actStatus = evt.Activity_Status__c;
            contactName = name;
            actFirm = firm;
            contactId = cId;
        }
    }

    public List<activityWrapper> getActivityList(){
        system.debug('***** public List');
        return actList;
    }
    public List<activityWrapperImp> getActivityListImp(){
        system.debug('***** public List');
        return actListImp;
    }
}