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
LuciferLucifer 

Scheduling Job

Hi All,

 

I have an object in which every record created 9 tasks. The owner of the task is not the the record owner but its a project manager which is a custom field in the Object. I wrote a trigger which would create tasks in such a way and will send email to the project manager.  So I wrote  a scheduling job which is supposed to run every night and send reminder emails to respected project manager of the record 2 days before over due. I'm not sure where its going wrong but I don't get the result. Can some one help me please?

 

 

global class EscalateAnnualEnrollmentTaskProcess implements Schedulable {
global void execute(SchedulableContext ctx)
{

System.debug('Starting');

DateTime Dt = System.Now();
Date D = Date.newInstance(Dt.year(),Dt.Month(),Dt.day());


system.debug('*********************************DT'+Dt);
system.debug('*********************************D'+D);

/* list of open annual enrollment requests */
List<Annual_Enrollment_Request__c> requestIds = new List<Annual_Enrollment_Request__c>();
requestIds = [SELECT Id FROM Annual_Enrollment_Request__c WHERE status__c != 'ACCEPT'];

system.debug('*********************************reqiestIds'+requestIds);
/* list of tasks associated with these requests near overdue activitydate - 2 */
List<Task> tasksOverdue = new List<Task>();
if(requestIds.size()>0 & requestIds.size()<>null){
tasksOverdue =[SELECT Id, WhatId, ActivityDate, Subject FROM Task WHERE WhatId IN :requestIds AND IsClosed = false AND ActivityDate <= :D.addDays(+2) ] ;
}
system.debug('*********************************D.addDays(-2)'+D.addDays(-2));
system.debug('*********************************taskOverdue'+tasksOverdue);


if(tasksOverdue.size()>0 & tasksOverdue.size()<>null){
for (Task t:tasksOverdue) {
/* lookup the object */
list <Annual_Enrollment_Request__c> req = new list <Annual_Enrollment_Request__c>();

req= [SELECT Id, Project_Manager__c, Director__c FROM Annual_Enrollment_Request__c WHERE Id = :t.WhatId ];
if(req.size() > 0){
for(Annual_Enrollment_Request__c request: req){
System.debug('Vamsi.t.projectmanager : ' + request.Project_Manager__c);
/* lookup the user and send the email*/
list<User> pm = new list<User>();
pm = [SELECT Id, Name, Email FROM User WHERE Id = :request.Project_Manager__c LIMIT 1];
if(pm.size()>0){
for(User projectmanager :pm){
sendemail(projectmanager, t);
}
}
/* lookup the user and send the email */
list<User> dir = new list<User>();
dir = [SELECT Id, Name, Email FROM User WHERE Id = :request.Director__c LIMIT 1];
if(dir.size() >0){
for(User director : dir){
System.debug('Vamsi.t.director : ' + director);
sendemail(director, t);
}
}
}
}
}
}

}
global void sendemail(User projectmanager, Task t)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string[] toaddress = New String[] {projectmanager.Email};
email.setSubject('Task Almost Overdue');
email.setPlainTextBody(t.Subject + 'is due ' + t.ActivityDate + ' and needs some attention.');
email.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[] {email});
}
}

Jeff MayJeff May

Would it be a good idea to use a time dependent WF / Email Alert for this so you don't have to debug and maintain so much code?

LuciferLucifer

I have seen that option, There is no email action for a time dependent work flow. We only have

1)Create new Task

2)new feild update

3)new outbound message

4)Select Existing option

Jeff MayJeff May

Wow. Sorry.  Never noticed that restriction on Tasks.  Back to the drawing board.