- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 10:22 AM
Hello All,
I'm trying to create a metric definition on task table, on assigned to field. But my matric should only calculate where assignment group is "Prep Group". I believe type should be script calculation duration. could anyone help me with what I'm doing wrong in this code.
if (!current.active) {
if (current.assignment_group == 'PREP group Sys_id')
createMetric();
}
function createMetric() {
var mi = new MetricInstance(definition, current);
if (mi.metricExists())
return;
var gr = mi.getNewRecord();
gr.field_value = value;
gr.calculation_complete = true;
gr.insert();
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 02:59 AM
I would be careful with directly querying sys_audit because of the impact on performance.
Try the following
answer = (function(mi){
var incidentGR = mi.current;
//Hard-coding sys-id to Help Desk for testing purposes
if(incidentGR.getValue('assignment_group') != '679434f053231300e321ddeeff7b12d8'){
//End the existing duration field
mi.endDuration();
//Don't allow the creation of a new metric instance
return false;
}
return true;
}(mi))
This works in my PDI - it'll create a metric for the assigned to as long as the assignment group is the sys_id I've specified. If it's not, it'll stop the metric
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 11:41 PM
(function calculateMetric(previous, current) {
// Ensure 'assigned_to' field is updated
if (previous.assigned_to == current.assigned_to) {
return; // Exit if there is no change in assigned_to
}
// Get the "Prep Group" sys_id (Modify if needed)
var prepGroupSysId = 'YOUR_PREP_GROUP_SYS_ID';
// Check if the assignment group is "Prep Group"
if (current.assignment_group == prepGroupSysId) {
// Calculate duration in seconds
var duration = (new GlideDateTime().getNumericValue() - new GlideDateTime(previous.sys_updated_on).getNumericValue()) / 1000;
return duration;
}
})();
✔️ If this solves your issue, please mark it as Correct.
✔️ If you found it helpful, please mark it as Helpful.
—
Shubham Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 11:57 PM
Hello @Shubham_Jain ,
I kept type as field value duration because when i kept type as script calculation it is not returning any value. But this code is returning value even when i changed assignment group to different group/different group assignee. it should only work for PREP group assignees. Yes, i'm replacing with sysid of prep group
'YOUR_PREP_GROUP_SYS_ID';
Thank You!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 12:02 AM
(function calculateDuration(task, metric) {
var duration = 0;
var audit = new GlideAggregate('sys_audit');
audit.addQuery('documentkey', task.sys_id); // Get history of the task
audit.addQuery('fieldname', 'assignment_group'); // Track only assignment group changes
audit.addQuery('newvalue', 'YOUR_PREP_GROUP_SYS_ID'); // Only track when it is assigned to PREP group
audit.addAggregate('SUM', 'duration');
audit.query();
if (audit.next()) {
duration = audit.getAggregate('SUM', 'duration'); // Get total time spent in PREP group
}
return duration; // Return calculated duration
})(task, metric);
✔️ If this solves your issue, please mark it as Correct.
✔️ If you found it helpful, please mark it as Helpful.
—
Shubham Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 12:12 AM
Hello @Shubham_Jain ,
Now it's not working for none of the groups. Do i have to add any Sysid here ?
audit.addQuery('documentkey', task.sys_id); // Get history of the task
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 12:25 AM
@SM123 yes please try
✔️ If this solves your issue, please mark it as Correct.
✔️ If you found it helpful, please mark it as Helpful.
—
Shubham Jain