2

This question already has an answer here:

I have a Lightning Component where users can choose how often an Apex Batch should be run. As options, I want to allow 30min, hourly and daily. Changing that should modify the CRON expression of a Scheduled job.

Here is an example: the user selects "every 30min" at 18th July 2019 10:34 A.M then the next scheduled run should be 18th July 2019 11:04 A.M.

Here is my Apex code:

if(period == 'Every 30 Minutes') {
    cron = '0 ' + System.now().addMinutes(30).minute() + ' * * * ?';
}
else if(period == 'Hourly') {
    cron = '0 ' + System.now().minute() + ' * * * ?';
}
else if(period == 'Daily') {
    cron = '0 0 ' + System.now().addHours(1).hour() + ' *' + ' *' + ' ?';
}

"Hourly" and "Daily" work as expected, but "Every 30min" doesn't as the first run successfully started 30min after the click, but the next one waited for one hour.

marked as duplicate by Robert Sösemann, Pranay Jaiswal, Raul, Vijay Ganji, glls Jul 19 at 11:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Simple answer: You can't schedule more often than hourly in Salesforce. – Robert Sösemann Jul 18 at 8:02
  • Hi @RobertSösemann, It ran successfully for the first time with a duration of 30 minutes. Would it be fine to calling the scheduler again in the finish method as it got executed correctly for the first time. – Pranav Chitrans Jul 18 at 8:05
5

The first time it runs after thirty minutes because you've set the "minute" part thirty minutes out. However, after that, it will only run hourly at that same marker.

Instead, you would need to schedule two jobs, one 30 minutes out, and one that waits an hour, or have the scheduler "suicide" (abort itself via System.abortJob) and schedule itself 30 minutes out, thus repeating the cycle.

It's impractical to have jobs that run less than hourly, and this is an intentional design choice to allow a fair amount of asynchronous resources to all orgs.

Depending on your use case, you might just write a scheduler that is set to run hourly on the hour, and that scheduler would consult a configuration object to determine which batches it needs to run in the next hour and schedule one-off jobs for those batches. This should give you to-the-minute capabilities and reliable scheduling.

  • Hi @sfdcfox Can you please explain the last paragraph. I got confused about that. I have written the batch class and in the finish method, I am calling again the system. schedule but that every time abort the other and then starts the other. – Pranav Chitrans Jul 18 at 17:24
  • It's impractical to have jobs that run less than hourly - I have 6 dashboard refresh jobs that run on 5,15,25,35,45,55 minute markers, respectively - not sure it was 'impractical' - just a pain to set up initially :-) – cropredy Jul 18 at 18:18

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