Metrics definition for certain group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 10:28 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 10:38 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 12:17 AM
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 12:15 PM
Hello @SM123
Your code will work fine. Here are the steps to adhere:
All > Metrics > Defintions (Table Name: metric_definition) > Click New
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:
Now the Incident is assigned to a different Assignment group with different Assigned To
You can notice the same metric definition did not have any recent entry in the metric_instance table
Hope that helps!
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 12:17 AM
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".