I see a lot of documentation on scheduling Apex to run repeatedly (i.e. every day at 1am), or even run once at a specific point in time, but I don't see anything about dynamically scheduling Apex to run, for example, 5 or 10 minutes from now.

My use case is that our Salesforce org will be receiving messages from an external system at random times, and every time it does, we need to store a token from that system, and pass it to a single scheduled Apex class that will run an hour later. So each class will be running with a different parameter, and at random points in time.

My first thought is that one could try to use system.now() and a Cron expression (i.e. String sch = '20 30 8 10 2 ?') to dynamically generate a Cron expression for each job that is scheduled, but that seems overly complicated.

Is there a way to simply specify in Apex "run this five minutes from now?"

Many thanks

1 Answer 1


The only "schedule X from now" is for Batchable:

System.scheduleBatch(new BatchClassImpl(), 'Near Future', 5);

This would schedule a job five minutes from now to run the batch job.

For anything else, you would indeed want to build your own cron string, which shouldn't be more than a dozen lines of code in most cases; you could even write a utility method if this is a common design across multiple Schedulable classes.

You could also technically use a Flow with a Pause element then call some Apex, or use Process Builder, but that involves metadata configuration instead of "just" Apex.

  • Thanks @sfdcfox. In that case, it almost makes sense to use a batchable class even though I don't really have use for the batching feature in this case.
    – number41
    Commented May 2, 2020 at 4:08
  • I'd say not because there is a limit to the number of async calls you have available per 24hrs per org and a batchable will consume 3 async calls (start, execute, finish) instead of just the one. You also have to provide data to be iterated in some way.
    – Phil W
    Commented May 2, 2020 at 8:28
  • @PhilW Yeah, I wouldn't exactly recommend it. But... someone who insisted on this could do all the work in the start method, abort the job, then return a null value. Neither the execute nor finish method run (so, 1 async call instead of 3), and you still get the easy scheduling.
    – sfdcfox
    Commented May 2, 2020 at 12:26
  • @sfdcfox How would he make sure the job scheduled by cron expression will run only once? By specifying the year?
    – EranV
    Commented May 2, 2020 at 13:46
  • 1
    @Eran Yes, you can create a cron expression specific enough to run just once that way.
    – sfdcfox
    Commented May 2, 2020 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .