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.

Metric Definition for priority being lowered

Patrick Cavanau
Tera Contributor

Hello All, 

I currently have a requirement to create a metric definition for incident priority that is lowered. I currently have the following code but it is not creating any metric definitions. Any help would be greatly appreciated!

PatrickCavanau_0-1731602850723.png

 

3 REPLIES 3

Community Alums
Not applicable

@Patrick Cavanau Your image of code is not clear for me can't read anything, but you should be having similar logic:

// Ensure both current and previous priorities exist

    if (!previous || !previous.priority || !current.priority) {

        return false;

    }

    // Check if the priority was lowered

    var currentPriority = parseInt(current.priority);

    var previousPriority = parseInt(previous.priority);

    if (currentPriority > previousPriority) {

        gs.info("Metric captured: Priority lowered from " + previousPriority + " to " + currentPriority);

        return true; // Metric will be recorded

    }

    return false;

 

Hope this will help you.

thank you for this. Unfortunately, It will not allow me to use return in the code for metric definitions

 

Regards

Community Alums
Not applicable

@Patrick Cavanau Please try below first in lower instance and this should work in metric definition:

// Ensure the Metric Instance is properly initialized

var mi = new MetricInstance(definition, current);

// Check if the metric instance exists and the priority has been lowered

if (!mi.metricExists()) {

    // Capture the previous priority state for comparison

    var prevPriority = current.priority.changes()[0]; // Get the previous priority value (if any)

 

    // Check if the priority was lowered (we compare previous and current priority)

    if (prevPriority && current.priority < prevPriority) { // Ensure priority is being lowered

        gs.info("Priority lowered for Incident " + current.sys_id + " from " + prevPriority + " to " + current.priority);

        

        // Start the duration when the priority is lowered

        mi.startDuration();

    }

}

// When the incident priority is updated, check if the metric instance exists and stop the duration

if (mi.metricExists() && current.priority != prevPriority) {

    gs.info("Priority change detected for Incident " + current.sys_id + " from " + prevPriority + " to " + current.priority);

    // End the duration for the priority change

    mi.endDuration();

}