- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2023 07:10 AM - edited ‎09-22-2023 07:11 AM
I'm new to scripting, so please excuse my inexperience.
Objective: to create a metric instance when an incident is reopened where the reopen_count > 0
Have tried: creating a field metric definition using the reopen_count field.
Issue: when a new incident gets created, an metric instant gets created where the reopen_count = 0
Can someone please help create a script that records when an incident is reopened but ignores when the reopen_count = 0 ?
Many thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2023 05:47 AM
This script appears to provide what I was looking for. Post updated in the hope it may help others:
if (parseInt(current.getValue("reopen_count"),10) > 0) {
var now = new GlideDateTime();
var mi = new GlideRecord("metric_instance");
mi.initialize();
mi.setValue("definition", definition.getValue("sys_id"));
mi.setValue("table", definition.getValue("table"));
mi.setValue("field", definition.getValue("field"));
mi.setValue("id", current.getValue("sys_id"));
mi.setValue("start", now);
mi.setValue("end", now);
mi.setValue("calculation_complete", true);
mi.setValue("value", current.getValue("reopen_count"));
mi.setValue("field_value", current.getValue("reopen_count"));
mi.insert();
}
Metric Instance data showing what is recorded:
- Same Incident was cycled between "in-progress" and "resolved" for testing purposes
- Each reopened event was recorded against the same incident
- This includes time (start) and value (the number of times it has been reopened)
- Can return the maximum value to find out how many times an incident has been reopened (in this case, 5 times).
Kind regards,
Sal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2023 09:26 AM - edited ‎09-22-2023 09:27 AM
Script include --> https://xxx.service-now.com/sys_script_include.do?sys_id=44c7c3a40a25810200e0dbdf70ea7f0c
replace "xxx" with your instance domain
Complete Script Include
gs.include("PrototypeServer");
var MetricInstance = Class.create();
MetricInstance.prototype = {
initialize: function(definitionGR, currentGR) {
this.definition = definitionGR;
this.current = currentGR;
},
// process is the driver for field value duration type definitions
process: function() {
answer = true;
mi = this; // global variable
eval(this.definition.script);
if (!answer)
return;
this.endDuration(); // end any previous duration for this metric
this.startDuration(); // start a new one
},
startDuration: function() {
var gr = this.getNewRecord();
gr.field_value = this.current[this.definition.field];
gr.start = current.sys_updated_on;
gr.insert();
},
endDuration: function() {
var gr = new GlideRecord('metric_instance');
gr.addQuery('definition', this.definition.sys_id);
gr.addQuery('id', this.current.sys_id);
gr.addQuery('calculation_complete', false);
gr.query();
if (!gr.next())
return;
gr.end = this.current.sys_updated_on;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.update();
},
updateDuration: function() {
var gr = new GlideRecord('metric_instance');
gr.addQuery('definition', this.definition.sys_id);
gr.addQuery('id', this.current.sys_id);
gr.addQuery('calculation_complete', false);
gr.query();
if (gr.next()) {
gr.value = this.current.reopen_count;
gr.update();
}
},
getNewRecord: function() {
var gr = new GlideRecord('metric_instance');
gr.table = this.current.getRecordClassName();
gr.id = this.current.sys_id;
gr.definition = this.definition.sys_id;
gr.field = this.definition.field;
return gr;
},
// return true if a metric exists for this definition and current
metricExists: function() {
var gr = new GlideRecord('metric_instance');
gr.addQuery("id", this.current.sys_id);
gr.addQuery("definition", this.definition.sys_id);
gr.query();
return gr.hasNext();
},
_z : function() {
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2023 05:52 AM
Thank you for your input, but I feel this proposed solution was overcomplicating what was needed. I have added a solution to this post that you may find helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2023 05:47 AM
This script appears to provide what I was looking for. Post updated in the hope it may help others:
if (parseInt(current.getValue("reopen_count"),10) > 0) {
var now = new GlideDateTime();
var mi = new GlideRecord("metric_instance");
mi.initialize();
mi.setValue("definition", definition.getValue("sys_id"));
mi.setValue("table", definition.getValue("table"));
mi.setValue("field", definition.getValue("field"));
mi.setValue("id", current.getValue("sys_id"));
mi.setValue("start", now);
mi.setValue("end", now);
mi.setValue("calculation_complete", true);
mi.setValue("value", current.getValue("reopen_count"));
mi.setValue("field_value", current.getValue("reopen_count"));
mi.insert();
}
Metric Instance data showing what is recorded:
- Same Incident was cycled between "in-progress" and "resolved" for testing purposes
- Each reopened event was recorded against the same incident
- This includes time (start) and value (the number of times it has been reopened)
- Can return the maximum value to find out how many times an incident has been reopened (in this case, 5 times).
Kind regards,
Sal