Calculate total 'on hold' duration of incident and add it as a custom field in the form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 06:22 AM
Hi,
We have a requirement to display the total time spent in 'on hold' state of an incident and display its value as a custom field in the incident form. I tried creating a 'duration' type field with calculated value using below script, but it does not seem to display the total duration calculated. However, when i use the same script on a 'string' type field, it populates the value. Can someone please help in identifying why 'duration' type field isn't displaying the value or where I am going wrong and how can I fix this. The duration is ideally required to be populated in day/hour/min format so its easily understandable by users.
Script :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 10:32 AM
Sorry @MaryJohn
its pause duration
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 03:57 PM - edited 02-17-2025 04:25 PM
If you look at how duration fields are stored, you see they are a value the represent number of seconds since '1970-01-01 00:00:00'. For example:
<duration>1970-01-01 00:15:53</duration>
this value is 15 minutes and 53 seconds. Duration fields are tricky to work with. Such as adding multiple values as you first posted. The available API is here:
I've tested the following script logic for metric_instance:
var mIns = new GlideRecord('metric_instance');
mIns.addEncodedQuery('definition=db763699833f5e102d24fa96feaad3d4^value=On Hold^calculation_complete=true');
mIns.query();
var sum = 0;
var epoch = new GlideDateTime('1970-01-01 00:00:00');
while (mIns.next()) {
var dur = mIns.duration.toString();
gs.info('epoch: ' + epoch + ', dur: ' + dur);
// need to get total seconds and API uses a schedule
var dc = new DurationCalculator();
dc.setSchedule('38fa64edc0a8016400f4a5724b0434b8'); //24x7
dc.calcScheduleDuration(epoch, dur);
sum += dc.getTotalSeconds();
gs.info('total seconds: ' + dc.getTotalSeconds());
}
// convert to Duration value
gs.info('sum: ' + sum + ', UTC Offset:' + epoch.getTZOffset());
var TZOffset = epoch.getTZOffset();
epoch.add(sum*1000);
gs.info('sum: ' + sum*1000 + ', TZOffset: ' + TZOffset + ', total: ' + ((sum*1000) + TZOffset));
gs.info('dur: ' + epoch.getDisplayValue());
// now add tz offset
epoch.add(-1*TZOffset);
gs.info('TZ adjusted dur: ' + epoch.getDisplayValue());
// GlideDateTime value to load a Duration field
// var dur = new GlideDateTime(epoch.getDisplayValue());
// return dur;
You can replace my query (lines 1-3) with your own for testing use in a Calculated field definition (uncomment the last two lines). And you can comment out or remove the 'gs.info();' lines used to debug.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 01:26 AM
Thank you for the solution @Bert_c1. This is really helpful!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 07:51 AM
You're Welcome @Anushiya Preeth , I hope this is useful to @MaryJohn , it took some time to get it right. Maybe it'll work in a Calculated field definition, the key is when it desired to run. a Client script/script include can work. a Business rule defined on the metric_instance with appropriate filter conditions can also work. And a fix script using the same logic can work.