Metri definition that tracks incidents resolved directly from an "On Hold" state.

CarolMa6
Tera Expert

Hi, 

Need some help. I created the metric and it triggers correctly, but it’s not calculating the duration, updating the end date, or setting calculation_complete to true. How can I configure this to work properly?

 

CarolMa6_0-1748268421751.png

 

Regards 

CarolMa

3 REPLIES 3

Abbas_5
Tera Sage
Tera Sage

Hello @CarolMa6,

 

To ensure your ServiceNow metric definition correctly calculates duration, updates the end date, and marks calculation as complete, verify the following configurations: 
 
  1. 1. Metric Definition:
    • Type: Ensure the "Type" field in your metric definition is correctly set. If it's a field value duration, the system will automatically handle end date and duration. If it's a script calculation, ensure the script properly sets the end time, duration, and calculation_complete to true.
    • Start Condition: Verify your start condition is accurately identifying the point where the metric should begin.
    • End Condition: Ensure your end condition is correctly identifying the point where the metric should end.
    • Duration Calculation: If you're using a script calculation, ensure your script uses the gs.dateDiff() function to calculate the duration between the start and end times. You may also need to convert the result to milliseconds.
    • End Time: Ensure the script updates the end field of the metric_instance with the current time (e.g., gs.nowDateTime()).
    • Calculation Complete: Set the calculation_complete field of the metric_instance to true.
    • Save: After making changes, remember to save the metric definition.
  2. 2. Business Rule (if applicable):
    • Trigger: Ensure the business rule triggers when the field that affects the metric is updated.
    • Script: If you're using a script in the business rule, verify it's accurately creating or updating the metric_instance record with the correct values.
  3. 3. Metric Instance:
    • End Date: The end field in the metric_instance record should be updated to the current time when the metric ends.
    • Duration: The duration field should be populated with the calculated time difference between the start and end times.
    • Calculation Complete: The calculation_complete field should be set to true when the duration is calculated.
Example Script (for script calculation type):
(function executeRule(current, previous /*null when async*/) {  // Check if the metric instance already exists  var mi = new GlideRecord('metric_instance');  mi.addQuery('definition', current.metric_definition.sys_id);  mi.addQuery('id', current.sys_id);  mi.query();  if (mi.hasNext()) {      mi.next();      //Update the existing metric instance       mi.end = gs.nowDateTime();       mi.duration = gs.dateDiff(mi.start, mi.end, true); //true for seconds, false for duration       mi.calculation_complete = true;       mi.update();  } else {      // Create a new metric instance       mi = new GlideRecord('metric_instance');       mi.initialize();       mi.definition = current.metric_definition;       mi.table = current.table;       mi.id = current.sys_id;       mi.start = previous.sys_created_on; //or other appropriate start time       mi.end = gs.nowDateTime();       mi.duration = gs.dateDiff(mi.start, mi.end, true); //true for seconds, false for duration       mi.value = current.u_your_field; // or your relevant field       mi.calculation_complete = true;       mi.insert();  }})(current, previous);
Troubleshooting:
 
  • Check the System Log:
    Review the System Log for any errors or warnings related to the metric definition or business rule.
  • Test and Debug:
    Create a test record and manually trigger the metric to see if it's working correctly.
  • Use the Debugger:
    If you're having trouble with the script, use the debug options in the ServiceNow platform to step through the script and see what's happening. 
     
By carefully reviewing these configurations and troubleshooting steps, you should be able to resolve the issue and ensure your ServiceNow metric definitions are calculating durations, updating end dates, and marking calculations complete accurately.
 
If it is helpful, please hit the thumbs up icon and accept the correct solution by referring to this solution it will be helpful to them.
 
Thanks & Regards,
Abbas Shaik

Ankur Bawiskar
Tera Patron
Tera Patron

@CarolMa6 

Did you check any of the existing OOTB metric definition script?

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

Hi @Ankur Bawiskar @Abbas_5 

 

Yes, I did check an existing OOB script. 

Created the below, now my issue is whenever the incident is on hold the metric is triggered and moving from on-hold to in progress also adds the metric. It should strictly add the metric when user moves the incident state from on-hold to resolved. 

 

I tried updating the script with the below 2 lines then the metric does not work at all 

CarolMa6_1-1748957823021.png

 

CarolMa6_0-1748957480394.png

Regards 

CarolMa