On Hold duration calculation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
I have the scenario, to calculate total duration when incident is put on hold and stop the calculation when incident state is inactive like closed or cancel how to achieve this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi Akshay,
Great question! Here’s how I implemented this:
Add 2 fields in the Incident table:
On Hold Duration → calendar_duration (type: Duration)
On Hold Start → u_on_hold_start (type: Date/Time)
Create 2 Business Rules:
1. Business Rule for Start Timer
Name: Business Rule for Start Timer
Table: Incident
Active: true
Advanced: true
When to Run: Before
Order: 100
Condition: State | Changes to | On Hold
- Update: checked
Script:
(function executeRule(current, previous) {
// When state changes to On Hold (3)
if (current.state.changesTo(3)) {
current.u_on_hold_start = new GlideDateTime();
}
})(current, previous);
2. Business Rule for Stop Timer
Name: Business Rule for Stop Timer
Table: Incident
Active: true
Advanced: true
When to Run: Before
Order: 100
Condition: State | Changes from | On Hold
Update: checked
Script:
(function executeRule(current, previous) {
// If previously On Hold and now changed to something else
if (previous.state == 3 && current.state != 3) {
if (previous.u_on_hold_start) {
var start = new GlideDateTime(previous.u_on_hold_start);
var now = new GlideDateTime();
var diffSeconds = gs.dateDiff(start.getValue(), now.getValue(), true);
// Add old duration if any
var oldDuration = current.u_on_hold_duration ? parseInt(current.u_on_hold_duration) : 0;
current.u_on_hold_duration = oldDuration + diffSeconds;
// Reset start time
current.u_on_hold_start = '';
}
}
})(current, previous);Testing Steps:
Make sure both business rules are Active.
Create a new Incident.
Add a Short Description.
Change State to In Progress and save.
Change State to On Hold → the On Hold Reason field will appear.
Select any option (e.g. Awaiting Change).
The On Hold Start field will populate with the exact time you saved the record.
Wait for a few seconds or minutes.
Change the state to Resolved/Closed.
Check the On Hold Duration field.
It will show the exact duration for which the incident was On Hold.
Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it helpful & accept the solution so others can benefit as well.
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
not getting the proper output.
When i keep incident for 5 mins on hold it should display in on hold duration field as 5 mins but it is showing like 32 days 18 hours.
Please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Yes I just checked! Thanks for clarification, Try using this:
(function executeRule(current, previous) {
// If previously On Hold and now changed to something else
if (previous.state == 3 && current.state != 3) {
if (previous.u_on_hold_start) {
var start = new GlideDateTime(previous.u_on_hold_start);
var now = new GlideDateTime();
// Difference in milliseconds (GlideDateTime gives ms)
var diffMillis = GlideDateTime.subtract(now, start);
// Create a GlideDuration object
var duration = new GlideDuration(diffMillis);
// Add old duration if any
if (current.u_on_hold_duration) {
var oldDuration = new GlideDuration(current.u_on_hold_duration);
duration.add(oldDuration);
}
current.u_on_hold_duration = duration; // Correct type
current.u_on_hold_start = '';
}
}
})(current, previous);Its working in my instance
Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it helpful & accept the solution so others can benefit as well.
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Still output not coming in my instance.
