Find your content:

Search form

You are here

Are there best practices to get scheduled apex scheduled to run faster than once per hour?

 
Share

When trying to set up a schedule to run every 5 minutes I got an error that seemed to indicate that you can't schedule apex to run every X minutes or every Y seconds. It appears the fastest you can go is once per hour.

Are there any best practices to get around this limitation (say, scheduling the job a few times) or is this restriction something that should really be adhered to?

You can have a max of 10 scheduled jobs - so theoretically if you only had 1 job that needed to run you could schedule 10 jobs, each starting 6 minutes apart. Not sure if that would work so well in practice, though.


Attribution to: Ryan Elkins

Possible Suggestion/Solution #1

I wouldn't call it best practice, it might even be frowned upon, but since you can schedule jobs from apex you have the ability to reschedule a job after the first one completes dynamically.

IE: if you have a job run and it takes 5 minutes, you can insert code into the finish method that will reschedule itself after a certain timeout. so instead of having to wait an hour you can conceivably reschedule the batch on a much shorter schedule.


Attribution to: ebt

Possible Suggestion/Solution #2

Since you can schedule a batch job from Apex I'd suggest placing the scheduler on an external service (AWS for instance) governed by cron or another timing solution and then calling your apex web service to trigger it off.


Attribution to: Steven Herod

Possible Suggestion/Solution #3

The limit is now 25 shceduled jobs instead of 10, meaning once every 2.4 minutes instead of 6 is the new maximum.

It is also possible to start a batch job from a scheduled apex job and then schedule a new job from batch apex opening the door to infinite chains of execution. If your batch job implements Database.Stateful it's even possible to persist object state between these!

If you're looking to write a little less code yourself I saw a post about an app called Skoodat Relax that may be worth looking into although I have little personal experience with it.

Last but not least there's the off-platform option of using an external service, say a simple timer app on heroku or even a *nix cron job that does a callout to your salesforce org on whatever schedule you want.


Attribution to: ca_peterson

Possible Suggestion/Solution #4

An alternative to using the Scheduler for jobs that need to run regularly:

  1. Create a Custom Object called "Batch Runner". Include a datetime field "Next Batch Start Time" indicating the next time the batch should run, as well as any fields for attributes you may need for your batch.
  2. Create a Trigger on your object that kicks off the batch class. Probably should check that there aren't too many batches running already.
  3. Create a Time Dependent Workflow triggering on "Next Batch Start Time" that does a field update of some kind to the Batch Runner object record, which will then trigger the batch to run.
  4. In the finish() method of your batch, reset the Batch Runner record so that the time-dependent workflow will kick off again.

I haven't been able to get time-dependent workflow to run in any shorter period than about 5-10 minutes because of queueing, but that seems to be the same kind of limitation that Scheduler has as well. To stop the infinite batch loop, you can delete the future time-dependent workflow field update, or modify the Batch Runner record so that the field update never occurs.


Attribution to: Jeremy Nottingham

Possible Suggestion/Solution #5

I have used this code long back.. Not sure if this will work for you.. You can probably have a look at it.

https://web.archive.org/web/20130622162551/http://bitplex.me/2010/05/scheduling-apex-jobs-every-10-minutes.html


Attribution to: Sathya
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/897

My Block Status

My Block Content