- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 10:43 AM
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.
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 09:41 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 10:51 AM
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:
- It retrieves the numeric values of the "Time elapsed duration" and "On hold duration" fields using the getNumericValue() method of the GlideDuration object.
- It subtracts the value of "On hold duration" from "Time elapsed duration" to calculate the MTTR value.
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 09:22 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 09:41 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 11:46 PM