There are several ways to do time tracking in ServiceNow. One of the ways used frequently (especially in Incident Management) is the ‘Time Worked’ field. The ServiceNow wiki describes this functionality. I often see the requirement to have some control over the stop/start of this Time Worked field for customers using this functionality. This post describes the approaches I’ve used in the past to meet this need.

Paused Time Worked Timer

Default Start/Stop state for Time Worked field for ALL Tasks

ServiceNow includes a simple property that allows you to specify whether the time worked timer should be started or paused when a task form loads. This property might meet the needs of some customers, but lacks the needed flexibility for most people using the time worked field. The property name is ‘glide.ui.timer.started’ and can be accessed under the ‘UI Properties’ module as shown here…
Pause Timer Property

Toggle Time Worked Client Script

If you need the additional flexibility of starting or stopping the time worked field for particular task types or even for particular states in given task types, you need to use a client script to start and stop the timer. One common requirement I’ve seen for this is to have the timer always be stopped if the form loads for an already-closed or resolved incident ticket. For this scenario, you could use the script I’ve created below. The script I’ve written takes 2 parameters…the first takes the name of the timer field you want to start or stop, and the second parameter (which is optional) allows you to set the timer to a specific time if needed.

function onLoad() {
    if(g_form.getElement('time_worked')){
        toggleTimer('time_worked','00:00:00');
    }
}

function toggleTimer(fieldName,timerVal){
    try{
        //Get the necessary timer elements for fieldName given
        var timerEl = $('element.' + g_form.tableName + '.' + fieldName);
        var timerIcon = $('link.' + g_form.tableName + '.' + fieldName);
        if (timerEl != null){
            //Make sure script does not break if field not present
            var timerPause = timerEl.select('input[id*=paused]')[0];
           
            //Toggle the timer
            if(timerPause.value == 'false'){
                timerPause.value = 'true';
                timerIcon = $('link.' + g_form.tableName + '.' + fieldName);
                timerIcon.removeClassName('icon-stop');
                timerIcon.addClassName('icon-play');
            }
            else{
                timerPause.value = 'false';
                timerIcon.removeClassName('icon-play');
                timerIcon.addClassName('icon-stop');
            }
            //Reset the timer to timerVal given
            if(timerVal){
                g_form.setValue(fieldName,timerVal);
            }
        }
    }catch(e){}
}