- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2025 11:39 AM
Greetings Community
On my company we have a Dictionary Entry with a custom script, to calculate the MI TTR, right not the script is this one
(function calculatedFieldValue(current) {
// Add your code here
var miStart = current.u_mi_start.getDisplayValue();
var servRest = current.u_service_restored.getDisplayValue();
var ttr = gs.dateDiff(miStart,servRest,false);
return ttr; // return the calculated value
})(current);
this makes a direct calculation between miStart and servRest, what we need is to have the lowes between miStart and detectionTime and the result with a dateDiff to have the TTR we have this script but is not working
(function calculatedFieldValue(current) {
// Add your code here
var miStart = current.u_mi_start.getDisplayValue();
var servRest = current.u_service_restored.getDisplayValue();
var detectionTime = current.u_detection_time.getDisplayValue(); // Assuming you have a detection time field
var ttr;
if (miStart > detectionTime) {
ttr = gs.dateDiff(detectionTime, servRest, false);
} else {
ttr = gs.dateDiff(miStart, servRest, false);
}
return ttr; // return the calculated value
})(current);
Anyone can help me to know why is not working as expected
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2025 03:08 PM - edited 02-28-2025 03:19 PM
Hello @joseflores3
I made some minor changes and it should work:
(function calculatedFieldValue(current) {
// Retrieve values as GlideDateTime objects
var miStart = current.u_mi_start.getGlideObject();
var servRest = current.u_service_restored.getGlideObject();
var detectionTime = current.u_detection_time.getGlideObject();
// Get the earliest time between miStart and detectionTime
var earliestStart = miStart.getNumericValue() < detectionTime.getNumericValue() ? miStart : detectionTime;
// Calculate TTR (Time to Restore)
var ttr = gs.dateDiff(earliestStart, servRest, false);
// Log the calculated values for debugging
gs.log("TTR_VJ1 Calculation: miStart = " + miStart);
gs.log("TTR_VJ1 Calculation: detectionTime = " + detectionTime);
gs.log("TTR_VJ1 Calculation: servRest = " + servRest);
gs.log("TTR_VJ1 Calculation: TTR = " + ttr);
return ttr; // Return the calculated value
})(current);
Dummy Test: (Selected date time is Pacific Standard Time)
After Business Rule
Logs (UTC): MI Start: 8:30 pm and service restored: 10:35 pm -- TTR: 2 hours 5 minutes
Hope that helps
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2025 01:19 PM
Hi @joseflores3
Please update your code as below and try if it works.
(function calculatedFieldValue(current) {
// Add your code here
var miStart = current.u_mi_start.getDisplayValue();
var servRest = current.u_service_restored.getDisplayValue();
var detectionTime = current.u_detection_time.getDisplayValue(); // Assuming you have a detection time field
var ttr;
if (detectionTime!="" && detectionTime!="undefined"){
if (miStart > detectionTime) {
ttr = gs.dateDiff(detectionTime, servRest, false);
}
}
else {
ttr = gs.dateDiff(miStart, servRest, false);
}
return ttr; // return the calculated value
})(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2025 01:53 PM
Thank you for your reply, I have validated and tested, and the Behavior is the same.
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2025 03:08 PM - edited 02-28-2025 03:19 PM
Hello @joseflores3
I made some minor changes and it should work:
(function calculatedFieldValue(current) {
// Retrieve values as GlideDateTime objects
var miStart = current.u_mi_start.getGlideObject();
var servRest = current.u_service_restored.getGlideObject();
var detectionTime = current.u_detection_time.getGlideObject();
// Get the earliest time between miStart and detectionTime
var earliestStart = miStart.getNumericValue() < detectionTime.getNumericValue() ? miStart : detectionTime;
// Calculate TTR (Time to Restore)
var ttr = gs.dateDiff(earliestStart, servRest, false);
// Log the calculated values for debugging
gs.log("TTR_VJ1 Calculation: miStart = " + miStart);
gs.log("TTR_VJ1 Calculation: detectionTime = " + detectionTime);
gs.log("TTR_VJ1 Calculation: servRest = " + servRest);
gs.log("TTR_VJ1 Calculation: TTR = " + ttr);
return ttr; // Return the calculated value
})(current);
Dummy Test: (Selected date time is Pacific Standard Time)
After Business Rule
Logs (UTC): MI Start: 8:30 pm and service restored: 10:35 pm -- TTR: 2 hours 5 minutes
Hope that helps
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 09:52 AM
Hi Vishal, thank you for your reply.
I tried this script and worked to identify the lowest but is sending the wrong result, i will share more details to the post with an example
Regards