Metric- Not working
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2024 05:30 AM
Hey Everyone,
I have been working on metrics to calculate the duration of assigned_to field for one the tables we have...The problem is where i am moving the record to analyze(state=2) , I want the existing value of assigne_to field should be picked up and insert record in metric instance table for duration ...this is happening only when I am manually changing the value at field level..could anyone pls suggest me any changes in my code here?
I am using two metrics one is for assigned_to and other to stop metric calculation at other states
Metric 1: Assigned_to duration
Type-Script Calculation
if (!current.assigned_to.nil()) {
createMetric();
}
function createMetric() {
// Create a new MetricInstance object
var mi = new MetricInstance(definition, current);
// Create a new record for the metric instance (no check for existing metric)
var gr = mi.getNewRecord();
gr.field_value = current.assigned_to;
gr.start = current.sys_updated_on;
gr.calculation_complete = false;
gr.insert();
}
Metric2 : Close Duration
Type : script Calculation
// Close Durations on Awaiting Approval, New,Approved Cancelled, Rejected or Expired State
if ( current.state == 8 || current.state == 3 || current.state == 9 || current.state == 7 || current.state == 1) {
closeDurations(current);
}
function closeDurations(current) {
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', false);
gr.addQuery('definition.name', 'Assigned to Duration');
gr.query();
while (gr.next()) {
var definition = new GlideRecord('metric_definition');
definition.get(gr.definition);
var mi = new MetricInstance(definition, current);
mi.endDuration();
}
}
// Process Metric Instances for Business Duration Calculation (24*5 Weekdays)
var gr = new GlideRecord("metric_instance");
gr.addEncodedQuery('end!=NULL^business_duration=NULL^definition.name=Assigned to Duration^table=sn_compliance_policy_exception');
gr.autoSysFields(false);
gr.query();
while (gr.next()) {
var gsBusiness = new GlideSchedule('80eb64431b7f6954c1cb4197dc4bcb8b'); // 24*5 Weekdays Schedule
if (gr.start && gr.end) {
gr.business_duration = gsBusiness.duration(gr.start.getGlideObject(), gr.end.getGlideObject());
gr.update();
}
}
Any kind of help is greatly appreciated!
Thank you,
0 REPLIES 0