Reopen ticket if not closed in 5 mins

PK14
Kilo Guru

Hi Team,

I need urgent assistance.I've created the scheduled job below, which runs every 1 minute (repeat interval: periodically).
The requirement is to set the state of the task to "Open" and clear the "Assigned to" field if the task is not closed within 5 minutes of its creation.

Example:
If a task is created at 1:00 PM IST and is not closed by 1:05 PM IST, then the system should update its state to Open and clear the Assigned to field.
Although the below code works, I'm facing an issue where the job performs the update before 5 minutes have passed. In some cases, it's reopening the task within just a few seconds of creation.

var taskGr = new GlideRecord('sc_task');
// Get current time and subtract 5 mins
var now = new GlideDateTime();
var fiveMinutesAgo = new GlideDateTime();
fiveMinutesAgo.addMinutes(-5);
// Query: Created before 5 mins ago
taskGr.addQuery('sys_created_on', '<=', fiveMinutesAgo);
taskGr.addQuery('assigned_to', '16562d1ddbcb9490daad7f93e296195f'); //ABC
taskGr.addQuery('assignment_group', '14d1f5446f86a100c80e175e5d3ee4c0'); //Support 
taskGr.addQuery('state', 'NOT IN', '3,4'); (closed complete or closed incomplete)
taskGr.query();
while (taskGr.next()) {
    if (taskGr.state != '1') {
        taskGr.state = '1'; // Open
        taskGr.assigned_to = ''; // Clear assignment
        taskGr.work_notes = "Auto-reopened: Not closed within 5 minutes of creation.";
        taskGr.update();
        gs.info('Task ' + taskGr.number + ' reopened after stuck > 5 mins');
    }
}
1 ACCEPTED SOLUTION

@PK14 

try to use addSeconds() method and give seconds for 5mins and 6 mins in that method

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

View solution in original post

9 REPLIES 9

Rafael Batistot
Tera Sage

Hi @PK14 

 

Your logic is almost correct, but the issue happens because of time zone mismatches and date precision problems with sys_created_on.

 

Why Is Your Job Acting Too Early?

 

Cause:

  • sys_created_on is stored in UTC, but GlideDateTime() returns the system’s timezone or GMT depending on context.
  • In scheduled jobs, GlideDateTime() is in UTC, so you’re safe there—but sys_created_on might not have millisecond precision, and replication delay can sometimes cause early matches.

 

Problem With <= fiveMinutesAgo:

 

  • Newly created tasks with sys_created_on rounded down to the nearest second might still fall within the <= fiveMinutesAgo condition even if they’re a few seconds old.
  • This causes false positives, reopening tasks too early.

 

 

Change:

taskGr.addQuery('sys_created_on', '<=', fiveMinutesAgo);

 

To:

taskGr.addQuery('sys_created_on', '<', fiveMinutesAgo);

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@PK14 

what's the business use-case to make it open again in 5mins?

wouldn't it have a load on your instance as you are constantly re-opening closed catalog tasks and also the job is running every 1min?

Issue with your script:

-> Your job is processing all tasks created before "now minus 5 minutes"—not just tasks created between 5 and 6 minutes ago.

-> your job runs every minute, so it will process tasks as soon as they're older than 5 minutes

Try this updated script

-> Process only those tasks whose sys_created_on is "between 5 and 6 minutes ago".

var taskGr = new GlideRecord('sc_task');

// Calculate window
var fiveMinutesAgo = new GlideDateTime();
fiveMinutesAgo.addMinutes(-5);
var sixMinutesAgo = new GlideDateTime();
sixMinutesAgo.addMinutes(-6);

taskGr.addQuery('sys_created_on', '>=', sixMinutesAgo);
taskGr.addQuery('sys_created_on', '<=', fiveMinutesAgo);
taskGr.addQuery('assigned_to', '16562d1ddbcb9490daad7f93e296195f'); // ABC
taskGr.addQuery('assignment_group', '14d1f5446f86a100c80e175e5d3ee4c0'); // Support
taskGr.addQuery('state', 'NOT IN', '3,4'); // closed_complete, closed_incomplete

taskGr.query();
while (taskGr.next()) {
    if (taskGr.state != '1') {
        taskGr.state = '1'; // Open
        taskGr.assigned_to = ''; // Clear assignment
        taskGr.work_notes = "Auto-reopened: Not closed within 5 minutes of creation.";
        taskGr.update();
        gs.info('Task ' + taskGr.number + ' reopened after stuck > 5 mins');
    }
}

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

Hi @Ankur Bawiskar ,

Tried using this, created a fresh task with the conditions but the job doesn't perform any action at all now. Ticket is still in work in progress state. 


@PK14 

did you add logs and debug?

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