The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Reopen stuck Tasks

PK14
Kilo Guru

Hi Team, 

My requirement is to set the state to open if the ticket is in work in progress state for more then 15 minutes (doesn't close) when a ticket is assigned to automation. 

I have written a schedule job which works but resets the ticket to open state before 15 minutes. I assume it is because of the "Repeat Interval" that has been set for 2 minutes. (Runs periodically). 

var now = new GlideDateTime();
var fifteenMinutesInMillis = 15 * 60 * 1000; // 15 minutes
// Query tasks assigned to automation and in 'Work in Progress'
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('assigned_to', '16562d1ddbcb9490daad7f93e296195f'); // automation
taskGr.addQuery('state', '2'); // Work in Progress
taskGr.query();
while (taskGr.next()) {
    // Find the most recent state change TO 'Work in Progress'
    var history = new GlideRecord('sys_history_line');
    history.addQuery('field', 'state');
    history.addQuery('name', taskGr.sys_id);
    history.addQuery('new_value', '2'); // State changed to Work in Progress
    history.orderByDesc('sys_created_on');
    history.setLimit(1);
    history.query();
    if (history.next()) {
        var stateChangeToWIPTime = new GlideDateTime(history.sys_created_on);
        var timePassed = now.getNumericValue() - stateChangeToWIPTime.getNumericValue();
        if (timePassed >= fifteenMinutesInMillis) {
            taskGr.state = 1; // Reopen the task
            taskGr.work_notes = "Task automatically reopened after 15+ minutes in 'Work in Progress' due to no closure action by automation.";
            taskGr.update();
        }
    }
}

Can you please assist on this. The task can be created anytime so based on that the scheduled job should work. 

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@PK14 

I will suggest not to rely on sys_history_line table, but use a custom field to check when state changed to WIP.

Steps

1) before update business rule

Condition: State [Changes To] Work In Progress

Script:

    current.u_wip_since = new GlideDateTime();

2) then update scheduled job and use that field for 15 mins check

Something like this

var now = new GlideDateTime();
var fifteenMinutesInMillis = 15 * 60 * 1000;

var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('assigned_to', '16562d1ddbcb9490daad7f93e296195f'); // automation sys_id
taskGr.addQuery('state', '2'); // Work in Progress
taskGr.addNotNullQuery('u_wip_since'); // Must have WIP start time
taskGr.query();

while (taskGr.next()) {
    var wipStart = taskGr.u_wip_since.getGlideObject();
    var timePassed = now.getNumericValue() - wipStart.getNumericValue();

    if (timePassed >= fifteenMinutesInMillis) {
        taskGr.state = 1; // Reopen the task
        taskGr.work_notes = "Task automatically reopened after 15+ minutes in 'Work in Progress' without closure by automation.";
        taskGr.u_wip_since = ''; // Reset the field
        taskGr.update();
    }
}

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

@PK14 

Hope you are doing good.

Did my reply answer your question?

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

@PK14 

Hope you are doing good.

Did my reply answer your question?

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,

Please let me know, if "u_wip_since" is field that storing the time when state changed to WIP?