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

added the logs.

There's no difference between fiveMinutesAgo and SixMinutesAgo. Both the timings fetched is same. 

@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

Ankur Bawiskar
Tera Patron
Tera Patron

@PK14 

Would you mind closing your earlier questions by marking appropriate response as correct?

Members have invested their time and efforts in helping you.

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

Viraj Hudlikar
Giga Sage

Hello @PK14 

var taskGr = new GlideRecord('sc_task');
var now = new GlideDateTime();
var fiveMinutesAgo = new GlideDateTime();
fiveMinutesAgo.addMinutes(-5);

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()) {
    var created = new GlideDateTime(taskGr.sys_created_on);
    if (created.getNumericValue() <= fiveMinutesAgo.getNumericValue()) {
        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 has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello @PK14 

 

Did you tried with my solution not sure if you had look on it or not.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.