I have a trigger that calls a schedulable class. The trigger fires on insert and I want to set my schedulable class to run 1 hour and 1 minute after the insert occurs.

I was calling my schedulable class like this:

OrderApprovalDeadlineScheduler.scheduleIt('0 1 1 * * ? *', newCollection);

I can see my apex job has been scheduled with a status of pending, but the it is well past the 1 hour and 1 minute and I would have expected the job to have run by now. Is my Cron Exp wrong?

Thanks for any help.


That is not actually scheduling it for 1 hr and 1 minute in the future but rather for 1:01 am in the morning every day. Probably not what you intended.

If you want to run it just 1 hour and 1 minute in the future, you could do something like the following:

Datetime futureTime = Datetime.now().addHours(1).addMinutes(1);
String futureTimeCronString = '0 ' + futureTime.minute() + ' ' + futureTime.day() + ' ' + futureTime.month() + ' * ' + futureTime.year();
System.schedule('OrderApprovalDeadlineScheduler', futureTimeCronString, new OrderApprovalDeadlineScheduler());

This has not been tested but should be close to what you want. It builds up a Cron Expression for a specific date and then schedules the class to run. It does assume that your OrderApprovalDeadlineScheduler class implements the Schedulable interface.

However, I would caution against actually setting up scheduled jobs from a trigger. It is potential that the trigger will get called over 100 times before one of the jobs runs, at which point you will have hit a governor limit and the process will fail. See the documentation here

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.