function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Leonardi KohLeonardi Koh 

Sheduler Strange Behaviour: System.AsyncException Based on configured schedule, the given trigger will never fire

Hi guys, i have a most peculiar behavior with Salesforce scheduler that i've never seen before
So i wrote a little code whose job is just to queue itself every minute to do some work
SomeClass m = new SomeClass();
datetime nextMinuteRun = datetime.now().addMinutes(1);

string sch = '0 '+ string.valueof(nextMinuteRun.minute()) + ' ' + 
string.valueof(nextMinuteRun.hour()) + ' ' + 
string.valueof(nextMinuteRun.day())
+ ' ' + string.valueof(nextMinuteRun.month()) + ' ? ' 
+ string.valueof(nextMinuteRun.year());
            
system.schedule('Keep Running ', sch, m);
This system works fine and keeps rescheduling itself every minute
.. most of the time that is because once in a while, the schedule will fail with the exception in the title and i'd have to restart the scheduler manually again.

Now my guess is that there are times when the system was scheduling itself at say 11:59 : 59
and that gives the next runtime schedule of 12:00 : 00 (a 1 second difference)
but during the process... by the time Salesforce reached the line to schedule the systtem, it was already at 12:00:00, and this causes the failure.

Now, what i am curious is... can Salesforce actually take THAT LONG before it processed the schedule line?
1 second sounds like a short time, but in computer processing that ought to be a long time... especially since this is a line by line code processing that normally would take a fraction of a second.
Best Answer chosen by Leonardi Koh
Deepali KulshresthaDeepali Kulshrestha
Hi Leonardi,

It turns out there was some sort of Salesforce bug. I was able to repeat the issue at will.
Here's the situation: I was scheduling a job from a scheduled job. However, because sometimes 
I have seen scheduled jobs get significantly delayed in their execution, even to the point where it doesn't execute until it's fire time is in the past (which throws "System.AsyncException: 
Based on the configured schedule, the given trigger will never fire."), I used a try-catch block, with the first system. schedule inside the try braces.
It turns out, when there is, in fact, such an AsyncException that gets caught, the apex job somehow is scheduled in spite of the caught exception. The obvious problem is however that it's scheduled for a past time that will never be reached. As a result, it hangs indefinitely in the apex jobs queue, 
but for some reason does not have a corresponding scheduled job to delete, and of course, the scheduled class is locked from updating until this is resolved. Feels like a bug to me.
I was able to resolve it by simply copying and deleting the schedulable class, which in turn deleted all corresponding apex jobs (including those which had completed, aborted, etc.). I was then able to easily 
re-create the class by pasting back in the code.
Instead of using a try-catch block, I am now using if statements to compare the scheduled run time with datetime .now(), and if the run time is in the past, I increment it by minutes at a time in a while loop until it becomes later than now. Then the job gets scheduled with the incremented run time.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Leonardi,

It turns out there was some sort of Salesforce bug. I was able to repeat the issue at will.
Here's the situation: I was scheduling a job from a scheduled job. However, because sometimes 
I have seen scheduled jobs get significantly delayed in their execution, even to the point where it doesn't execute until it's fire time is in the past (which throws "System.AsyncException: 
Based on the configured schedule, the given trigger will never fire."), I used a try-catch block, with the first system. schedule inside the try braces.
It turns out, when there is, in fact, such an AsyncException that gets caught, the apex job somehow is scheduled in spite of the caught exception. The obvious problem is however that it's scheduled for a past time that will never be reached. As a result, it hangs indefinitely in the apex jobs queue, 
but for some reason does not have a corresponding scheduled job to delete, and of course, the scheduled class is locked from updating until this is resolved. Feels like a bug to me.
I was able to resolve it by simply copying and deleting the schedulable class, which in turn deleted all corresponding apex jobs (including those which had completed, aborted, etc.). I was then able to easily 
re-create the class by pasting back in the code.
Instead of using a try-catch block, I am now using if statements to compare the scheduled run time with datetime .now(), and if the run time is in the past, I increment it by minutes at a time in a while loop until it becomes later than now. Then the job gets scheduled with the incremented run time.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
This was selected as the best answer
Matt McGrawMatt McGraw
Thank You, Deepali!

We've had immense frustration w/ this error due to Execution time delaying past Scheduled time.

But we still need help! Can you please clarify (with code ideally) how you did this?  "...I am now using if statements to compare the scheduled run time with datetime .now(), and if the run time is in the past, I increment it by minutes at a time in a while loop until it becomes later than now."