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
Raj R.Raj R. 

How to schedule a Schedulable class to run every hour?

Hi, I have the following schedulable class below and i wanted to be able to schedule it to run at 12am through the UI by navigating to schedule apex and then setting it to run every day at 12am. However, after this job has ran, i want it to schedule a subsequent job (same class) to run in the next hour. Is this possible?

The goal is to only schedule it through the UI to run at 12 Am and then it will automatically schedule the remaining jobs 1 hour later.
 

global class SampleClass implements Schedulable{
    global void execute(SchedulableContext alistContext) {
        Database.executeBatch('myBatchClass', 100);

       //when database executebatch is done, I want to schedule the same job 1 hour later
       String cron_exp = '0 0 1 * * ?';
       String jobName = 'somename';
       System.Schedule(jobName, cron_exp, new SampleClass())
    }
}
Best Answer chosen by Raj R.
Shashikant SharmaShashikant Sharma

Hi,

What you need to do is:

1. Instead of rescheduling it in Schedule Class you should reschedule it in final method of the the myBatchClass
2. Just put following code in your final method of the batch class
 
Datetime sysTime = System.now();

       // this would increase the time to 1 hour after current time
        sysTime = sysTime.addHours(1);

        String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        
        
        //Schedule the next job, and give it the system time so name is unique
        System.schedule('New Schedule' + sysTime.getTime(),chron_exp, new SampleClass());



You can read this blog for more infomration https://corycowgill.blogspot.in/2010/12/leveraging-scheduled-apex-to-link-batch.html

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma

Hi,

What you need to do is:

1. Instead of rescheduling it in Schedule Class you should reschedule it in final method of the the myBatchClass
2. Just put following code in your final method of the batch class
 
Datetime sysTime = System.now();

       // this would increase the time to 1 hour after current time
        sysTime = sysTime.addHours(1);

        String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        
        
        //Schedule the next job, and give it the system time so name is unique
        System.schedule('New Schedule' + sysTime.getTime(),chron_exp, new SampleClass());



You can read this blog for more infomration https://corycowgill.blogspot.in/2010/12/leveraging-scheduled-apex-to-link-batch.html

Thanks
Shashikant
This was selected as the best answer
Raj R.Raj R.
Hi Shashikant,

What happens when the Database.executeBatch spawns several instances of the batch class up to 100 records? Will it schedule only 1 or won't it schedule for every batch "sub process" that is created?
Shashikant SharmaShashikant Sharma
Hi,

final method of a Batch is executed only once in complete Batch execution once all batch executions are processed. So say there are 1000 records and  batch size is 10 then there would be 100 batch will be executed with 10 records in each. But still final will only execute once after all 100 executions are done so it will only schedule next job only once.

I hope above answers your question.

Thanks
Shashikant
Raj R.Raj R.
Hi Shashikant,

Yes that answers my questions and resolved my concern. Thank you very much.
Christina ZhankoChristina Zhanko
You have no need to do this work manually. Our company developed solution for problems like that. Try this https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000DqCmYUAV. It allows you to execute your schedule at a specified frequency, even every 5 minutes.

P.S. It's free
Ankita Kudithipudi 25Ankita Kudithipudi 25
Hi Sashikanth,
Thank you for the code and blog link you provided.This helped me as well.I have one more question.So according to the code I suppose the batch process runs every hour without break.is that correct.I mean this is what I am trying to achieve.But what if I have to stop the job from running in the next hour?
Thank you
Ankita