Ho to calculate on hold time for an SLA attached to a task

zabeeulla2
Kilo Expert

Hi All,

I have a requirement where I need to calculate the on hold time for an SLA attached for a task. when a task is put in on hold state   The SLA   should not get paused and the SLA time should be calculated until the state is changed to some other state. Again if the SLA is put on hold from some other state then I need to display the Total time for which the SLA was in On hold state.

Please help me in solving this issue.

Immediate help is appreciated.

Regards,

Zabeeulla.

7 REPLIES 7

Geoffrey2
ServiceNow Employee
ServiceNow Employee

If you're not updating the Task SLA record, then do you even need to do anything with it at all?   It sounds like you could do this by recording the time the State was changed to On Hold, and then when the State is changed again, calculate the time different between the on-hold time and the current time and display it to the user.



If I'm wrong, please provide more detail.


Hi Geoffrey,


Could you please help me with the script that how could I calculate the duration for which the ticket was on hold state, The time should contain the sum of all the times the ticket was on hold state.



Regards,


Zabeeulla.


Geoffrey2
ServiceNow Employee
ServiceNow Employee

You will need to create 2 new fields on the form and make them read-only:


  • On hold since [u_on_hold_at] (Type: Date time) - For tracking when the State was change to On Hold
  • On hold duration [u_on_hold_duration] (Type: Duration) - For tracking the total On Hold time


You will then need 2 Business Rules:


  • When: before update
    Condition: current.state.changesTo(3)
    Script: current.u_on_hold_at = gs.nowDateTime();

  • When: before update
    Condition: current.state.changesFrom(3)Script:

if (current.u_on_hold_duration.nil()) {


      // Calculate the duration from u_on_hold_at


      current.u_on_hold_duration = gs.dateDiff(current.u_on_hold_at.getDisplayValue(), gs.nowDateTime(), false);


} else {


      // Calculate the duration in seconds from u_on_hold_at, then add it to the existing total


      var seconds = gs.dateDiff(current.u_on_hold_at.getDisplayValue(), gs.nowDateTime(), true);


      var gdt = new GlideDateTime(current.u_on_hold_duration);


      gdt.addSeconds(seconds);


      current.u_on_hold_duration = gdt.getTime();


}


current.u_on_hold_at = '';


Daniel Draes
ServiceNow Employee
ServiceNow Employee

Check out Metrics: Metrics



There is a default metric which would count the time per state for a given task record. This way you can easily report how long the task was 'on hold'. You might need than some database view to join the task_sla with the metric_instance table to get both numbers in one report. If the default metric dies not help, try creating your own metric having your custom business logic.