Dictrionary Entry - Script not working as expected

joseflores3
Tera Contributor

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

 

1 ACCEPTED SOLUTION

Vishal Jaswal
Giga Sage

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)
 

vishal_jaswal_0-1740784479195.png



After Business Rule

vishal_jaswal_1-1740784507857.png

vishal_jaswal_2-1740784530908.png


Logs (UTC):  MI Start: 8:30 pm and service restored: 10:35 pm -- TTR: 2 hours 5 minutes

vishal_jaswal_3-1740784549465.png

 

 

 

Hope that helps

 


Hope that helps!

View solution in original post

5 REPLIES 5

Swapna Abburi
Mega Sage
Mega Sage

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);

Hi @Swapna Abburi 

 

Thank you for your reply, I have validated and tested, and the Behavior is the same.

 

Regards

Vishal Jaswal
Giga Sage

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)
 

vishal_jaswal_0-1740784479195.png



After Business Rule

vishal_jaswal_1-1740784507857.png

vishal_jaswal_2-1740784530908.png


Logs (UTC):  MI Start: 8:30 pm and service restored: 10:35 pm -- TTR: 2 hours 5 minutes

vishal_jaswal_3-1740784549465.png

 

 

 

Hope that helps

 


Hope that helps!

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