3

I need to have a job that runs every 15 minutes. I checked out the documentation for Apex Scheduler and I came up with two solutions:

1-st:

ScheduledJob job = new ScheduledJob();
System.schedule('Scheduled Job - 1', '0 0 * * * ? *', job);
System.schedule('Scheduled Job - 2', '0 15 * * * ? *', job);
System.schedule('Scheduled Job - 3', '0 30 * * * ? *', job);
System.schedule('Scheduled Job - 4', '0 45 * * * ? *', job);

2-nd:

System.schedule('Scheduled Job - 1', '0 0 * * * ? *', new ScheduledJob());
System.schedule('Scheduled Job - 2', '0 15 * * * ? *', new ScheduledJob());
System.schedule('Scheduled Job - 3', '0 30 * * * ? *', new ScheduledJob());
System.schedule('Scheduled Job - 4', '0 45 * * * ? *', new ScheduledJob());

Both examples work. I see that 1st solution shares the instance of the ScheduledJob, while the other creates a separate instance for each schedule. The question is which one is better or appropriate? Or does it matter at all?

  • I'd go w/ #2 if only to future proof if you want to pass parameters into the constructor that might be different for job 1 v job 2 v job 3 v job 4 – cropredy May 31 at 13:25
3

Salesforce schedules the class for execution at the specified time. Actual execution may be delayed based on service availability.

Based on the above it is possible that 2 or more jobs can start at the same or nearly same time.
In other words - neither option you described guarantees that the job will actually be started every 15 minutes.

Consider following questions:
- Does the logic in your "ScheduledJob" class account for multiple concurrent jobs?
- Is there a chance that single job execution takes more than 15 minutes?

Option #3
3.A. If the job you need to schedule is a Batch Apex job then schedule next job from finish() method of Database.Batchable

3.B. If your job is not Batch Apex then schedule next job from the appropriate place at the end of your scheduled job logic

This way you can guarantee that there is no concurrent jobs and better chance to have them actually run with at least 15 minutes interval.

You will also need to decide how to start the very first job and also consider the case when currently running scheduled job fails and therefore the whole chain stops (because finish() is not called if the job did not complete)

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.