The CreatorCon Call for Content is officially open! Get started here.

Metric definition script calculation code working on Dev and test instance but not on Production

GarimaU
Tera Expert

Requirement: Create metric definition for Assignment group 'ABCD', calculated duration between 'accepted state-2' to 'resolved-6' state

Issue: The metric works fine on lower instance but not on production, for same scenarios. Cache was cleared

below is the code

// variables available:
// current: GlideRecord - the incident
// definition: GlideRecord - the metric definition

var TARGET_GROUP = 'SUP-WW-NIS-ServiceNow';
var ACCEPTED_STATE = '2';
var Resolved_STATE = '6';

// Only when incident is now closed and assigned to the target group
gs.info("Metric Debug: Starting metric definition for incident " + current.number + " (" + current.sys_id + ")");
if (current.incident_state == Resolved_STATE && current.assignment_group.name == TARGET_GROUP)
 {
    gs.info("Metric Debug: Incident " + current.number + " meets the initial conditions (state is 'Resolved' and group is 'ABCD').");



    var acceptedTime = getAcceptedTimestampIfGroupMatched(current.sys_id);

    if (acceptedTime) {
        createMetric(acceptedTime, current.sys_updated_on);
    }
}

function getAcceptedTimestampIfGroupMatched(incidentSysId) {
    var audit = new GlideRecord('sys_audit');
    audit.addQuery('documentkey', incidentSysId);
    audit.addQuery('fieldname', 'incident_state');
    audit.addQuery('newvalue', ACCEPTED_STATE);
    audit.orderBy('sys_created_on');
    audit.query();

    while (audit.next()) {
        // Check on what group was assigned at the time of acceptance
        var history = new GlideRecord('sys_history_line');
        history.addQuery('document', incidentSysId);
        history.addQuery('field', 'assignment_group');
        history.addQuery('sys_created_on', '<=', audit.sys_created_on); // group BEFORE or AT time of acceptance
        history.orderByDesc('sys_created_on');
        history.setLimit(1);
        history.query();

        if (history.next()) {
            var groupName = getGroupName(history.new_value);
            if (groupName == TARGET_GROUP) {
                return new GlideDateTime(audit.sys_created_on); // return accepted timestamp
            }
        }
    }
    return null;
   
}

function getGroupName(groupSysId) {
    var grp = new GlideRecord('sys_user_group');
    if (grp.get(groupSysId)) {
        return grp.name;
    }
    return '';
}

function createMetric(startTime, endTimeObj) {
    gs.info("Metric Debug: Entering createMetric function with start time " + startTime + " and end time " + endTimeObj);
    var mi = new MetricInstance(definition, current);
    if (mi.metricExists())
    { gs.info("Metric Debug: Metric instance already exists. Aborting creation.");
   
        return;
    }
    var endTime = new GlideDateTime(endTimeObj);
    var gr = mi.getNewRecord();
    gr.start = startTime;
    gr.end = endTime;

    gr.duration = gs.dateDiff(startTime.getDisplayValue(), endTime.getDisplayValue());
    gr.calculation_complete = true;
    gr.insert();
    gs.info("Metric Debug: Successfully created metric record with sys_id: " + metricSysId + " and duration: " + gr.duration);
}
0 REPLIES 0