0

I have a controller of an Apex Page from where I am calling a schedule apex class using system.schedule to run after one hour. From that schedule class, a batch class is being called to run which has email sending functionality.

Ideally, I am expecting those emails to be received one hour after my operation in controller.

Though I find a schedule job being created at the perfect time, the emails are received instantly after the schedule class called from controller.

Can someone please help me figure out the issue?

Controller code

 BatchScheduleUpdate bcn = new BatchScheduleUpdate (selcall[0]) ;
                    String hour = String.valueOf(Datetime.now().hour() + 1);
                    String min = String.valueOf(Datetime.now().minute()); 
                    String ss = String.valueOf(Datetime.now().second());
                    String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';
                    System.schedule('Job Started At ' + String.valueOf(Datetime.now()), nextFireTime, bcn);

Schedule Class:

global class BatchScheduleUpdate implements Schedulable
{   private Call__c modCall;

 global BatchScheduleUpdate (Call__c modCall) {
        modCall = modCall;
    }
    global void execute(SchedulableContext sc)
    {   
        if(modcall!=null)
       { Notbatch b = new Notbatch (modCall);
         database.executebatch(b);
       }

    }

}
1

Your class will fail if current hour value is 12, than it will equal to 13 and fail to schedule class.

Try to set scheduler like this:

Datetime fireTime = Datetime.now().addHours(1);
String cron = fireTime.second() + ' ' + fireTime.minute() + ' ' + fireTime.hour() + ' * * ?';
System.schedule('Test schedule run', cron, new BatchScheduleUpdate());
2
  • Thanks a lot. But why is it instantly running the emails? I need to run them 24 hours after. Also, I am getting this compile error. Error: Compile Error: Method does not exist or incorrect signature: void addHour(Integer) from the type Datetime at line .... – Testing_SFDC Jul 24 '19 at 10:00
  • @Testing_SFDC addHours() my mistake, sorry. That can be explained if you have mismatch in timezone on your machine and timezone set in your org or user. – Mariia Illarionova Jul 24 '19 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.