In scheduled Job retry functionality when job fails

SumanthBabu
Tera Contributor

Hi, ServiceNow Family, Good Evening

Note : I am using this scheduled job in scoped application
I have created retry functionality function in scheduled job. when we will get other than 200 https status code. I need to re-execute the scheduled job after 5 mins. But when I executed am facing below error

Error : Cannot find function addMinutes in object 2025-02-20 13:56:01.

Logs error : 

com.glide.script.RhinoEcmaError: Cannot find function addMinutes in object 2025-02-20 13:56:01.
<refname> : Line(320) column(0)
317: job.name = 'EWM testing'; // Set the name to the current job's name

318:

319: var gdt = new GlideDateTime();

==> 320: gdt.addMinutes(5); // Add 5 minutes to the current time

321:

322: job.next_action = gdt.getValue(); // Use the internal value of GlideDateTime


My scheduled Job function:

function rescheduleJob() {
// Retrieve the current job's trigger record based on job name (e.g., 'EWM testing')
var currentJob = new GlideRecord('sys_trigger');
currentJob.addQuery('name', 'APP testing'); // Scheduled Job Name
currentJob.orderByDesc('sys_created_on');
currentJob.setLimit(1);
currentJob.query();

// Check if the current job exists and determine if we can reschedule it
if (currentJob.next()) {
var retryCount = currentJob.retry_count || 0;

if (retryCount < 3) {
// Increase retry count by 1
currentJob.retry_count = retryCount + 1;
currentJob.update();

// Re-schedule the job after 5 minutes
var job = new GlideRecord('sys_trigger');
job.initialize();
job.name = 'EWM testing'; // Set the name to the current job's name

var gdt = new GlideDateTime();
gdt.addMinutes(5); // Add 5 minutes to the current time

job.next_action = gdt.getValue(); // Use the internal value of GlideDateTime

job.script = currentJob.script; // Use the script from the existing job
job.retry_count = retryCount + 1; // Set the new retry count
job.insert();
} else {
gs.error('Job retried 3 times, not re-executing anymore.');
}
} else {
gs.error('Failed to find the current job record for rescheduling.');
}
}



Thanks in advance

4 REPLIES 4

Kieran Anson
Kilo Patron

Hi,

addMinutes isn't a function available. You'll need to use addSeconds and do the math

.addSeconds(60 * 5)

Ankur Bawiskar
Tera Patron
Tera Patron

@SumanthBabu 

Please use script shared by @Kieran Anson and it should work

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

BillMartin
Mega Sage

Hi @SumanthBabu ,

 

Why do you need to create a custom retry mechanism? ServiceNow's out-of-the-box queuing technology, specifically through Event Registry, already offers built-in auto-retry capabilities. Could you share your use case? Understanding the context will help determine if the standard functionality meets your needs or if a custom solution is necessary.

BillMartin
Mega Sage

Hi @SumanthBabu ,

 

I have created an example of ServiceNow Queueing capability in the link below..... Specific to your need, you can use gs.eventQueueSchedued() object, where out of the box asynchronous retries are implemented.