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

@PK14 

yes it's the time when State Changes to WIP, that's what is mentioned in my earlier response

AnkurBawiskar_0-1753186529427.png

 

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

Pranesh072
Mega Sage
Mega Sage

You can create a flow with trigger as state change to WIP. Use wait for duration action then reassign or end the flow according to your need.