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
chowdary marellachowdary marella 

Schedular apex

Hi all i had written a class, due_date__c is date/Time field in Lead
Requirement:-I need due date reminders sent to whomever sets the due date. For example, if an agent completes a task then sets a date for the next action, a reminder needs to go to that agent to remind him/her that they have tasks to complete. Reminders should come the day before the event is due, again at 3 hours before due date and then one hour before due date.

need to schedule before 3hrs,before 1day and before 1 hour.

QUdos in Advance..Thank uu

 

 

My code

 

global class Sending_Email_SINGLECLASS implements Schedulable
{
public List<Lead> led=new List<Lead> ();
public list<lead> led1= new List<Lead> ();
public List<Lead> led2=new List<Lead> ();
list<id> leadid=new list<id>();

global void execute(SchedulableContext ctx)
{

Datetime d=system.TODAY();

 led=[select id,DUEDATETIME__c,Email from lead where DUEDATETIME__c=:date.valueOf(d.addHours(-1))];
system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+led);
led1=[select id,DUEDATETIME__c,Email from lead where DUEDATETIME__c=:date.valueOf(d.addHours(-2))];
system.debug('@++++++++++++++++++++++++++++++++++++++'+led1); */
led2=[select id,Due_Date__c,Email from lead where Due_Date__c=:date.valueOf(d.adddays(-1))];
system.debug('@*************************************'+led2);



for(Lead l:led)
{
leadid.add(l.id);
system.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'+leadid);
}


for(Lead l:led1)
{
leadid.add(l.id);
system.debug('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'+leadid);
}


for(Lead l:led2)
{
leadid.add(l.id);
system.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'+leadid);
}
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(leadid); // assign the all emails
//mail.setReplyTo('exemple@exemple.ex');
EmailTemplate et= [SELECT id FROM EmailTemplate WHERE developerName = 'birth_day'];
mail.setSenderDisplayName('SirdikChowdary Marella'); // displaying the senders name
mail.setTemplateId(et.id); // adding the templete id,here we are taken subject,body.
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
system.debug('********mail *@@@@@@@'+mail );
}
}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can't guarantee that the email will be sent an hour before. The system has a granularity of approximately 15 minutes, so it will be slightly off from an hour before regardless. Scheduled Apex will be no better in this respect; the system runs scheduled apex code when there are free resources.

All Answers

sfdcfoxsfdcfox
Why not use a Workflow Rule with Time-Dependent actions instead? You don't need to write any code at all, and there's no need to schedule a class to run periodically; the system will manage all the details for you.
chowdary marellachowdary marella

Hello .Thanx a lot for your reply,,i done as u said.. created 3 Time-Dependent Workflow Actions with 3rd option Evaluate the rule when a record is created, and any time it’s edited to subsequently meet criteria,”  RULE:----(Lead: DUEDATETIMENOT EQUAL TOnull) AND (Lead: EmailNOT EQUAL TOnull).     But my problem is If suppose the due date is edited from suppose 16/7/2013 to 18/7/2013 ,,, how it will manage becoz am taking rule as email and due date!=0...how to send email only on latest update date....please make out ,,can it possible through this??????

sfdcfoxsfdcfox
When the Due Date is modified, the rule will be re-evaluated, and the latest dates will be used automatically. You don't need to worry about that situation.
chowdary marellachowdary marella
My problem is in Time based workflow.it not considering minutes.suppose my
due date is 3/15/2013 9:00 PM.it is sending mail(1 hour time based
workflow) at 8:00 pm fine as i expects..but when my due date is 3/15/2013
9:10 PM. it is not sending any mail. i think bcoz of minutes,,,,,,but i
want to send exactly 1 hour before
sfdcfoxsfdcfox

You can't guarantee that the email will be sent an hour before. The system has a granularity of approximately 15 minutes, so it will be slightly off from an hour before regardless. Scheduled Apex will be no better in this respect; the system runs scheduled apex code when there are free resources.

This was selected as the best answer
chowdary marellachowdary marella

Hi fox, its working.but facing a issue with 1hour .

i have created 3 time based workflow rules as 1hr,3hr,1day.

 

iam getting 3 emails for  1hour before rule. iam not getting wats wrong. but rest are fine 

1 hour before rule is triggering thrice,

chowdary marellachowdary marella

I am getting all 3 emails(1HR,3HR,1DAY) before 1hour. and 2emails (1DAY,3HRS) before 3 hours, and one email before 1 day .becoz all the 3 conditions satisfy..so please suggest me,to send only mail at one time

sfdcfoxsfdcfox
You should have just one workflow rule with three entries, each with one email action. You may want to contact support if you can't seem to get it just right; they can login and look at your setup.