Help with metric script

veronica_malmo
Kilo Expert

Hi,

I have a metric script that should calculate the duration from a change is created and to it's in the state: proposed, sub_state: awaiting_customer_proposal, but something is not working with it. I'm not the one that has created this metric in the first place, I just hoped I could use it.. can anyone see any obvious mistakes?

// variables available

// current: GlideRecord -   target Change

// definition: GlideRecord -   (this row)

var s = current.state;

var ss = current.u_sub_state;

// We attach a metric when Change is created

if (current.sys_updated_on == current.sys_created_on)

createMetric();

// If Change state reaches Proposed and Sub-State Awaiting Customer approval we close the metric.

if(s == -7 && ss == 'awaiting_customer_approval')

closeMetric();

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 = false;

gr.insert();

}

function closeMetric() {

var gr = new GlideRecord('metric_instance');

gr.addQuery('id', current.sys_id);

gr.addQuery('calculation_complete', false);

gr.addQuery('definition.type','calculation');

gr.addQuery('definition',definition.sys_id);

gr.query();

while(gr.next()){

// gs.log("==>>DEBUG. Found existing Problem metric. Updating");

// gs.log("==>>DEBUG. closing: "+ gr.definition.name+" for: "+ current.number);

gr.calculation_complete = true;

gr.end = current.sys_updated_on;

gs.log("==>>DEBUG. CHANGE KPI Start: " + gr.start.getDisplayValue());

gs.log("==>>DEBUG. CHANGE KPI End: " + gr.end.getDisplayValue());

gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());

gs.log("==>>DEBUG. " + gr.duration);

gs.log("==>>DEBUG. Updating Metric: " + gr.id);

gs.log("==>>DEBUG. current.sys_id: " + current.sys_id);

// var definition =new GlideRecord('metric_definition');

// definition.get(gr.definition);

// var mi =new MetricInstance(definition, current);

// mi.endDuration();

gr.update();

}

}

8 REPLIES 8

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

What exactly is not working?


No changes shows in the list, but I have created a test case that should meet the criterias..



find_real_file.png


Where is the current script? In a business rule?


It's in the metric definition


find_real_file.png