Metric Definition Issue

Cirrus
Kilo Sage

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

 

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();
}
1 ACCEPTED SOLUTION

Cirrus
Kilo Sage

 I ended up using 

var stat = current.getValue("status");

if (stat == "5")

    createMetric();

View solution in original post

5 REPLIES 5

J Siva
Tera Sage

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

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Cirrus 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur, as with Siva's response, this fails to generate any metric when the status = 5