11

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.

11

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.

1

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.

0

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.

  • Hmm, so that wouldn't really sue the scheduler - you'd just start it manually once and at the end of the code write something to wait (or not) and run again - so it just runs infinitely, right? Definitely an interesting option. – Ryan Elkins Aug 26 '12 at 22:28
  • A warning that I've seen developers do this in the past and end up with undeletable scheduled jobs as a result. This may have been fixed by now, but be aware it's almost certainly not an officially supported use. – ca_peterson Aug 27 '12 at 2:05
  • @ca_peterson You weren't referring to me when you said this, were you? The trouble I was having deleting the scheduled job was unrelated, a mixed DML issue. I haven't seen this undeleteable scheduled job issue using this technique, but I haven't made it into production yet... So, if you know of someone else that's had it I'd love to know about! Fully aware this is a bit hack :) – mjgallag Sep 4 '12 at 14:25
  • 1
    I was not, saw an unrelated support case at my day job a while back where this happened. Hopefully the underlying bug has been fixed but my warning was really that this is not something salesforce tests. – ca_peterson Sep 4 '12 at 16:23
0

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.

  • 1
    I once built something like this but in the end I abandoned it because it's incredibly imprecise: time based workflow is evaluated whatever salesforce feels like it, something like every 15 minutes usually. Much, much less granular than scheduled apex. On top of that if you start to really stress it you start (or did at least) getting strange "No fileforce server" errors. All in all I don't see a reason to use this approach over scheduled apex in any real world scenario. – ca_peterson Aug 27 '12 at 20:09
0

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

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.