
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2025 05:15 AM
Afternoon,
I have a created a metric definition where Field is Status and Type is Script Calculation. Status 5 = Ready, and I want to measure the duration from the time at which the ticket was created to the time it reaches State = 5. The problem I am having is that every time I create a new ticket (Draft = 1) it triggers the metric and returns a duration of 0. Logging confirms the value of stat = 1. Can anyone advise what's wrong please
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2025 04:25 AM
I ended up using
var stat = current.getValue("status");
if (stat == "5")
createMetric();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2025 05:20 AM - edited ‎03-12-2025 05:22 AM
Hi @Cirrus
In the second line if condition, you need to compare the value by using "==" .
Modified script:
var stat = current.status;
if (stat == '5')
createMetric();
function createMetric() {
var mi = new MetricInstance(definition, current);
if (mi.metricExists())
return;
var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
gr.end = current.sys_updated_on;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.insert();
}
Hope this helps.
Regards,
Siva

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2025 05:44 AM
Thanks Siva. Tried this and now no metric is triggered. But I can see from adding a log entry at line 2 that the script is being run
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2025 05:25 AM
try this and use toString() during comparison
var stat = current.status.toString();
if (stat == '5')
createMetric();
function createMetric() {
var mi = new MetricInstance(definition, current);
if (mi.metricExists())
return;
var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
gr.end = current.sys_updated_on;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.insert();
}
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
‎03-12-2025 05:46 AM
Ankur, as with Siva's response, this fails to generate any metric when the status = 5