Notify users when scheduled job fails to execute
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2024 05:03 AM
When the scheduled job fails to execute, we need to trigger a notification stating scheduled job failed. I understand we can check the status of scheduled job in sys_trigger table where we can see state and next action but still not sure how to check if the scheduled job fail to execute as i see the state of job is always ready.
Thanks in advance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 01:58 AM - edited ‎02-07-2024 02:00 AM
Hello,
This is a very interesting idea, but i don't think there is a simple answer to it. The reason behind this is on how scheduled jobs work and how error handling within ServiceNow works.
A scheduled job within service now is - in simplified terms - nothing more than a script include. Difference being, that there is a job, which checks what scheduled jobs to run. This then triggers the "script include" - aka the job. Now, when thinking about this, i had to confirm 2 things:
1) A scheduled job is not triggered through the event queue
2) A scheduled job is not aborted by an error in the script
Now the second topic is actually well discussed here: Solved: State field in Scheduled jobs shows ready always - ServiceNow Community What you see isn't the state of the job itself, it is the state of the trigger in the sys_trigger table. This is separate form your script. BUT, as discussed here: How do we find the failures in the sys_trigger tab... - ServiceNow Community, you can indeed see which jobs failed in the past (error count) and what the last failure was (last error).
Short Note: The state of the trigger is not telling as jobs with errors are just rescheduled again (which is also why you won't find an entry in the state "error").
It also answers the first point: Scheduling a job does not create anything in the event queue.
Why is that important? One way to react to an event in the event queue is to have a notification triggered (this is what we want). BUT, we first need an event. On the other hand, it is important to understand, that an error in scheduling a job (the trigger entry) does not equate to an error within the job equate to the state "error" (again, because it is rescheduled).
Long story short: How can we make sure that an aborted job will trigger an event?
Well, there is not a lot we can do. First, you can get an overview of your jobs here: Understand your Scheduled Jobs dashboard (servicenow.com) But this won't tell you, if a scheduled job failed (again, a trigger failing is not the same as a job failing).
And with that, this becomes a coding architecture topic. I am a big fan of separation of concern. A scheduled job should always call a script include. Why? Because now i can set up scheduled jobs that i do want to be notified about in the following way:
try {
// call the script include function
} catch(error){
gs.eventQueue("scheduledJob.ErrorEvent",null,error.message, param2); // -> this will generate an event
}
Lastly, i need to create an register my error event and create an event action to trigger the notification (where i can also parse my error message).
There does not seem to be an easy way around it (and maybe i am wrong about this), but the above way is how i would tackle the topic.
The alternative is, to create a business rule on the sys_trigger table which fires an event every time a entry increases its error count. => THIS MAY BE A QUICKER SOLUTION. Have not tried this though. The handling of sys_trigger records within ServiceNow is kind of wonky.
tl:dr -> You have two options: Explicit error handling within the script (bit more complex) OR implicit error handling through a business rule.
Regards
Fabian