50

Using the salesforce GUI it seams impossible to schedule a job every 15 minutes. Or more than once a day.

Schedule Job GUI

55

Jobs cannot be scheduled in increments smaller than one hour. Please read the documentation to learn about how you can schedule jobs that run in more complex patterns than the GUI allows.

In Apex, you can schedule four jobs to every hour, staggered 15 minutes apart:

System.schedule('Scheduled Job 1', '0 0 * * * ?', new ScheduledClass());
System.schedule('Scheduled Job 2', '0 15 * * * ?', new ScheduledClass());
System.schedule('Scheduled Job 3', '0 30 * * * ?', new ScheduledClass());
System.schedule('Scheduled Job 4', '0 45 * * * ?', new ScheduledClass());
  • 4
    and consider use of Developer Console to execute said Apex via 'execute anonymous apex'. – cropredy May 30 '14 at 18:41
  • The shorter version listed here does not work for apex cron – fehays Sep 11 '14 at 16:33
  • hi, where should i add this? in the scheduler class? thanks – sfdclearner Aug 12 '16 at 2:46
21

@Christian is correct, but you can combine all that into a single line to be more efficient.

System.schedule('Scheduled Job 1', '0 0,15,30,45 * * * ?', new ScheduledClass());

Edit

It appears I was incorrect, you can only use comma separated lists for units hours or larger. Minutes and seconds does not support a comma separated list, so it appears you are stuck with either scheduling 4 jobs like below, or getting creative by scheduling a following job in the Finish() method of your batch class.

System.schedule('Scheduled Job 1', '0 0 * * * ?', new ScheduledClass());
System.schedule('Scheduled Job 2', '0 15 * * * ?', new ScheduledClass());
System.schedule('Scheduled Job 3', '0 30 * * * ?', new ScheduledClass());
System.schedule('Scheduled Job 4', '0 45 * * * ?', new ScheduledClass());
  • Can you go as far with this technique as to schedule to run every 10 seconds? Perhaps you could comment/answer on that on this question salesforce.stackexchange.com/questions/37306/…. – Keith C May 30 '14 at 14:14
  • Hmmmmm. interesting. I think it would let you schedule it, but I highly doubt it will be successful as the job isn't guaranteed to actually run when its scheduled, its just queued I believe. – Chris Duncombe May 30 '14 at 14:33
  • 2
    I get an error System.schedule('Scheduled Job 1', '0 0,15,30,45 * * * ?', new NotiflyTrialExpiration()); System.StringException: Seconds and minutes must be specified as integers: 0 0,15,30,45 * * * ? – Shanker Paudel Aug 8 '14 at 15:02
11

I've been playing around with something like this to achieve periodic execution of an operation with granularity as low as 1 or 2 minutes. It's a class that implements the Schedulable interface. In its "execute" method, it simply reschedules a new instance of itself some time in the future (specified by the "intervalMinutes" parameter, then aborts its currently scheduled instance, and invokes a future method or launches a batch job to do the actual work. The nice thing about this approach is that it doesn't require multiple scheduled Apex jobs (there's never more than one scheduled at a time). It seems to be working quite well. See any potential drawbacks to this approach?

global without sharing class JobRunner implements Schedulable {
    Integer intervalMinutes;
    public JobRunner(Integer intervalMinutes) {
        this.intervalMinutes = intervalMinutes;
    }
    public void execute(SchedulableContext sc) {
        // Re-schedule ourself to run again in "intervalMinutes" time
        DateTime now  = DateTime.now();
        DateTime nextRunTime = now.addMinutes(intervalMinutes);
        String cronString = '' + nextRunTime.second() + ' ' + nextRunTime.minute() + ' ' + 
            nextRunTime.hour() + ' ' + nextRunTime.day() + ' ' + 
            nextRunTime.month() + ' ? ' + nextRunTime.year(); 
        System.schedule(JobRunner.class.getName() + '-' + now.format(), cronString, new JobRunner(intervalMinutes));
        // Abort the current job
        Id jobId = sc.getTriggerId();
        System.abortJob(jobId);     
        // Launch a batch job or call a future method to do the actual work
        Database.executeBatch(new SomeBatchJob());
    }
}
  • 3
    Drawback I see in this approach is - it will show each Apex job as aborted in the Apex job log – Prasanna Feb 9 '15 at 10:47
  • 1
    Good point. I guess it comes down to trade offs. If you really need granular scheduling down to one or two minutes, the platform doesn't really allow you to achieve it any other way AFAIK, unless you want to tie up a bunch of your scheduled Apex slots with multiple scheduled executions. – tlfu Feb 10 '15 at 11:42
  • I'm curious if there's a way to address the drawback @eprasu pointed out? For example, can the 'aborted' status of the job be overwritten with a 'success' status once we've programmatically determined everything went according to plan? – smohyee Jul 24 '15 at 16:28
  • Like @tlfu mentioned, this is a good solution if you don't want to tie up multiple slots per job. For example, running two jobs every minute (2*60>100) already brings you over the limit for scheduled jobs. Using tlfu's method, you only consume 2 jobs. I actually ended up going with this. – Sam Kamensky May 10 at 22:41
3

I've always found it difficult to schedule a job using Cron Expression. It always takes me a while to form a corn expression.

Recently I came across an online tool to generate cron expression called as Cron Maker. It's a superb tool which can be used to generate cron expression on the basis of minutes, hours, Daily, Weekly, Monthly and Yearly.

Refer below snapshot:

enter image description here

  • 2
    Regrettably, you can't schedule in increments smaller than an hour in Salesforce Cron Jobs. See the Accepted Answer for the relevant workaround. – sfdcfox Apr 20 '17 at 5:38
  • @sfdcfox Thanks for correcting. Updated the answer. – Rohit Mourya Apr 20 '17 at 6:30
0

Piggy-backing off other people's answers, here's a slightly more dynamic way of scheduling classes. It just requires the name of the class and every nth minute you want the class to run.

//You just need to modify these two variables
Integer everyNthMinute = 5;
String nameOfSchedulableClass = 'YourSchedulableClassNameHere';


Integer counter = 1;
for(Integer minute=0;minute<60;minute++){
    if(Math.mod(minute,everyNthMinute)==0){
        String scheduleName = nameOfSchedulableClass + String.valueOf(counter);
        String cronPattern  = '0 '+ String.valueOf(minute) +' * * * ?';
        System.schedule(scheduleName,cronPattern, (Schedulable)Type.forName(nameOfSchedulableClass).newInstance());
        counter++;
    }
}


protected by Community Aug 18 '17 at 14:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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