Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Calculate Incident on hold duration

Pooja68
Tera Contributor

The requirement is to calculate the on hold duration of an incident. Whenever an incident state changes from on hold , it should populate the total on hold time in a new field 'On hold Clock'.

The code I am using reflects first on hold time when my incident goes on hold for second time , but its not working for first time, tried putting a delay but did not work.

Before BR

Table: incident, Condition: state changes from On hold

gs.sleep(8000);
var timems =0;
var tm = 0;
var gr=new GlideRecord('metric_instance');
gr.addQuery('id',current.sys_id);
gr.addQuery('value','On Hold');
gr.query();
while(gr.next()){
tm = gr.duration.dateNumericValue();
timems += tm;

}
current.u_on_hold_clock.setDateNumericValue(timems);

 

Please suggest the modification to make it work in first time itself.

Thanks

Pooja

6 REPLIES 6

Tony Chatfield1
Kilo Patron

Hi, using any form of sleep() in your code is not advisable, as it can (and will) have a detrimental impact on your instance performance.

 It would be a better utilization of system resources to either

Add another field to your incident of type date\time, EG u_paused_time
Then in a beforeBR, if the incident is placed on hold set u_paused_time with the paused date\time.
if the incident is taken of hold, calculate the hold time (current time – u_paused_time) and add this value to your existing ‘On hold Clock’ field;
Then reset u_paused_time to '', NULL or epoch (depending on your preference).

 Another other option would be an OLA\SLA that tracks hold time and an afterBR on task_sla which would update incident ‘On hold Clock’ with the pause time.

The additional incident update this solution requires would still be much better for your operational environment than locking a worker with sleep().
As a bouns the OLA\SLA will manage schedules for you without any additional coding.

Thanks Tony,

Add another field to your incident of type date\time, EG u_paused_time
Then in a beforeBR, if the incident is placed on hold set u_paused_time with the paused date\time.
if the incident is taken of hold, calculate the hold time (current time – u_paused_time) and add this value to your existing ‘On hold Clock’ field;

This logic worked.

 

 

Hello @Pooja68 

Would you please share the final code here? I have also similar requirement

Sohail Khilji
Kilo Patron

Please join the below SERVCENOW INDIA FORUM @Telegram App

This group is focused on growing Servicenow skills/Learning/Quries/Meetups/Certifications/dumps/courses/interview/MockTest etc..


Servicenow India Forum


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect