Calculated time diff between two Duration fields

sreenivas8
Tera Contributor

Hi All,

 

I have two customized duration fields 1. Time elapsed in duration 2. On hold duration.

 

Now my requirement is, I need to calculate time difference between above two duration fields and insert the output to another duration filed i.e MTTR.   

MTTR= Time elapsed duration minus on hold duration.

 

Pls note,here all the field types are duration only.

 

I'm try with below update BR, but not getting desired input. pls help me.

 

sreenivas8_0-1689096728983.png

 kindly refer below snap as well...

Here my desire output is MTTR= Time elapsed minus on hold duration, then value(MTTR) should be 1 day 1 hour but here value I'm getting as empty. 

 

sreenivas8_1-1689096956643.png

 

 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sreenivas8 

try this

var dura = current.u_time_elapse.dateNumericValue();
var durb = current.u_on_hold_duration.dateNumericValue();
current.u_mttr_of_mi.setDateNumericValue(dura - durb);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Punit S
Giga Guru

Based on your requirement, you want to calculate the Mean Time To Repair (MTTR) by subtracting the "On hold duration" from the "Time elapsed duration." Here's an example of a Business Rule (BR) script that you can use to achieve this:

(function executeRule(current, previous) {
  var timeElapsed = current.time_elapsed_duration.getGlideObject().getNumericValue();
  var onHoldDuration = current.on_hold_duration.getGlideObject().getNumericValue();
  
  // Calculate MTTR by subtracting onHoldDuration from timeElapsed
  var mttrValue = timeElapsed - onHoldDuration;
  
  // Set the calculated MTTR value to the MTTR field
  current.mttr_duration = mttrValue.toString();
})(current, previous);

 

Here's how the script works:

  1. It retrieves the numeric values of the "Time elapsed duration" and "On hold duration" fields using the getNumericValue() method of the GlideDuration object.
  2. It subtracts the value of "On hold duration" from "Time elapsed duration" to calculate the MTTR value.
  3. Finally, it sets the calculated MTTR value as a string to the "MTTR duration" field.

Make sure to replace the field names (time_elapsed_duration, on_hold_duration, mttr_duration) with the actual field names in your instance. Additionally, ensure that the Business Rule is triggered appropriately (e.g., on insert, update, or any other necessary condition).

Once the Business Rule is active, it should calculate the MTTR based on the time elapsed and on hold durations and populate the "MTTR duration" field accordingly.

 

 

Please mark my answer as a solution/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.

Thanks,
Punit

sreenivas8
Tera Contributor

Hi @Punit SS  Thank you very much for your detailed explanation. I will certainly try this and let you know the outcome. however, here MTTR field type also duration, not a string. we are building KPI using these duration values in another BI tool. So, if we push the value into a string, then it might be not possible to other fields to calculate using string value, right? So, how to push the output into a MTTR duration format only? pls help on that part as well.

Ankur Bawiskar
Tera Patron
Tera Patron

@sreenivas8 

try this

var dura = current.u_time_elapse.dateNumericValue();
var durb = current.u_on_hold_duration.dateNumericValue();
current.u_mttr_of_mi.setDateNumericValue(dura - durb);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you @Ankur Bawiskar ji , it was a great help for me.  

 

Thanks a lot again 🙂