Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Can you please explain this script?

vellanki
Kilo Contributor

hi guys..

I have seen this script in the metric definition....could you please explain this..

here current is incident record..

// script can set answer to false to terminate processing of the metric

// mi - MonitorInstance
// answer
if (!current.active) {
answer = false;
mi.endDuration();
closeDurations(mi.current);
}

function closeDurations(current) {
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', true);
gr.addQuery('definition.type', 'field_value_duration');
gr.query();
while (gr.next()) {
var definition = new GlideRecord('metric_definition');
definition.get(gr.definition);
var mi = new MetricInstance(definition, current);
mi.endDuration();
}
}

is this retrieving records from incident that are open?

 

1 ACCEPTED SOLUTION

G Balaji
Kilo Guru

Hi,

 

// script can set answer to false to terminate processing of the metric

// mi - MonitorInstance
// answer
if (!current.active) { //if the active field in the current record has the value "false", 
answer = false;      //set answer to false
mi.endDuration();  //call endDuration() function , this function is in Script Includes
closeDurations(mi.current); call the function closeDurations by passing current record
}

 

function closeDurations(current) {
var gr = new GlideRecord('metric_instance');  //glide record metric instance table
gr.addQuery('id', current.sys_id);   //get the record whose id matches current sysid and
gr.addQuery('calculation_complete', true);  the field value "calculation complete" to true
gr.addQuery('definition.type', 'field_value_duration'); and "definition type" as "field value duration"
gr.query();
while (gr.next()) {
var definition = new GlideRecord('metric_definition'); //glide through metric definintion table
definition.get(gr.definition); 
var mi = new MetricInstance(definition, current);
mi.endDuration();
}
}

View solution in original post

3 REPLIES 3

gauravgurbani7
Giga Expert

I believe above script gives you the duration through metric about type of fields of incident ( gr.addQuery('definition.type', 'field_value_duration'); )   used on incident . going by the first line of code and as you said this is for incident i dont think it  retrievs records when incident is Active.

Lets say for an incident  you choose group A -5 and group B- 2 time in your entire ticket flow . At the end after incident gets inactive this will give you time duration of Group A was used there in your incident .

G Balaji
Kilo Guru

Hi,

 

// script can set answer to false to terminate processing of the metric

// mi - MonitorInstance
// answer
if (!current.active) { //if the active field in the current record has the value "false", 
answer = false;      //set answer to false
mi.endDuration();  //call endDuration() function , this function is in Script Includes
closeDurations(mi.current); call the function closeDurations by passing current record
}

 

function closeDurations(current) {
var gr = new GlideRecord('metric_instance');  //glide record metric instance table
gr.addQuery('id', current.sys_id);   //get the record whose id matches current sysid and
gr.addQuery('calculation_complete', true);  the field value "calculation complete" to true
gr.addQuery('definition.type', 'field_value_duration'); and "definition type" as "field value duration"
gr.query();
while (gr.next()) {
var definition = new GlideRecord('metric_definition'); //glide through metric definintion table
definition.get(gr.definition); 
var mi = new MetricInstance(definition, current);
mi.endDuration();
}
}

tq so much