On Hold duration calculation

akshaykcc19
Tera Contributor

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?

 

9 REPLIES 9

M Iftikhar
Tera Sage

 

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)

MIftikhar_0-1759413762233.png

 

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
    MIftikhar_2-1759413986009.png

     

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

    MIftikhar_3-1759414015741.png

     

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:

  1. Make sure both business rules are Active.

  2. Create a new Incident.

  3. Add a Short Description.

  4. Change State to In Progress and save.

    MIftikhar_4-1759414072482.png

     

  5. 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.

      MIftikhar_5-1759414160666.png

       

  6. Wait for a few seconds or minutes.

  7. Change the state to Resolved/Closed.

  8. Check the On Hold Duration field.

    • It will show the exact duration for which the incident was On Hold.

      MIftikhar_6-1759414212475.png

       

Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it helpful & accept the solution so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

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

 

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

MIftikhar_0-1759486104711.png

Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it helpful & accept the solution so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

@M Iftikhar 

Still output not coming in my instance.