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.

Metrics definition for certain group

SM123
Tera Expert

Hello All,

I'm here after Solved: Re: Metrics definition for certain group - ServiceNow Community  

I'm trying to enhance my metric definition. basically, my metric definition will capture the assigned to duration for only "prep group" assignment group members for incident table. i want last assignee's (i mean assigned to person before tickets get reassigned to other group) duration to be empty. Kindly help me with the following code

answer = (function(mi) {
    var incidentGR = mi.current;

    if (incidentGR.getValue('assignment_group') != 'Prep Group sysid') { 
        // End the existing duration field
        mi.endDuration();

        // Find the last metric instance for the same incident and clear its duration
        var metricGR = new GlideRecord('metric_instance');
        metricGR.addQuery('reference', incidentGR.getUniqueValue()); // Link to the same incident
        metricGR.addQuery('duration', '!=', ''); // Ensure we target non-empty durations
        metricGR.orderByDesc('sys_created_on'); // Get the latest metric instance 
        metricGR.query();

        if (metricGR.next()) {
            metricGR.setValue('duration', ''); // Clear the duration field
            metricGR.update(); // Save changes
        }

        // Don't allow the creation of a new metric instance
        return false;
    }

    return true;
}(mi));

 @Kieran Anson please check it out 

5 REPLIES 5

Medi C
Giga Sage
Giga Sage

@SM123 

Could you please try the following:

answer = (function(mi) {
    var incidentGR = mi.current;

    var prepGroupSysId = 'your-prep-group-sys-id'; // Replace this with the actual sys_id of the "Prep Group"

    // Check if the incident is assigned to a member of "Prep Group"
    if (incidentGR.getValue('assignment_group') != prepGroupSysId) {
        // End the existing duration field for non-prep group assignees
        mi.endDuration();

        // Find the last metric instance for the same incident and clear its duration
        var metricGR = new GlideRecord('metric_instance');
        metricGR.addQuery('reference', incidentGR.getUniqueValue()); // Link to the same incident
        metricGR.addQuery('duration', '!=', ''); // Ensure we target non-empty durations
        metricGR.orderByDesc('sys_created_on'); // Get the latest metric instance
        metricGR.query();

        if (metricGR.next()) {
            // Clear the duration for the last assignee's metric instance
            metricGR.setValue('duration', ''); 
            metricGR.update(); // Save changes
        }

        // Return false to prevent the creation of a new metric instance
        return false;
    }

    // Allow the creation of the metric instance if the group is "Prep Group"
    return true;
})(mi);

If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

@Medi C 

sometimes it's working sometimes it's not. I have tested the following scenario

First i ticket to some other group then again i reassigned to "Prep Group" and changed assignees in the same group. then again i reassigned ticket to some other random group. I see that duration is not empty for the last assignee of "PREP group"

Vishal Jaswal
Giga Sage
Giga Sage

Hello @SM123 

Your code will work fine. Here are the steps to adhere:

All > Metrics > Defintions (Table Name: metric_definition) > Click New

vishal_jaswal_0-1741119009360.png

answer = (function (mi) {
   var incidentGR = mi.current;
   if (incidentGR.getValue('assignment_group') !== 'sys_id of prep group')   {
       // End the existing duration field
       mi.endDuration();
       // Find the last metric instance for the same incident and clear its duration
       var metricGR = new GlideRecord('metric_instance');
       metricGR.addQuery('reference', incidentGR.getUniqueValue()); // Link to the same incident
       metricGR.addQuery('duration', '!=', ''); // Ensure we target non-empty durations
       metricGR.orderByDesc('sys_created_on'); // Get the latest metric instance
       metricGR.query();
       if (metricGR.next()) {
           metricGR.setValue('duration', ''); // Clear the duration field
           metricGR.update(); // Save changes
       }
       // Don't allow the creation of a new metric instance
       return false;
   }
   return true;
})(mi);


Validate in All > Metric > Instances (Table Name: metric_instance). 

Here is an example screenshot:

vishal_jaswal_1-1741119090953.png

vishal_jaswal_2-1741119166315.png


Now the Incident is assigned to a different Assignment group with different Assigned To

vishal_jaswal_3-1741119272802.png


You can notice the same metric definition did not have any recent entry in the metric_instance table

vishal_jaswal_4-1741119283677.png

Hope that helps!

 




Hope that helps!

@Vishal Jaswal ,

sometimes it's working sometimes it's not. I have tested the following scenario

First i assigned ticket to some random group then again i reassigned to "Prep Group" and changed assignees in the same group. then again i reassigned ticket to some other random group. I see that duration is not empty for the last assignee of "PREP group".