- 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-04-2025 02:00 AM
Perfect! then try this updated script
(function calculateMetric(task, metric) {
var totalDuration = 0;
var previousTime = null;
var prepGroupSysId = null;
// Step 1: Fetch "Prep Group" sys_id dynamically
var prepGroup = new GlideRecord('sys_user_group');
prepGroup.addQuery('name', 'Prep Group');
prepGroup.query();
if (prepGroup.next()) {
prepGroupSysId = prepGroup.getUniqueValue();
gs.info('Found Prep Group sys_id: ' + prepGroupSysId);
} else {
gs.info('Prep Group not found, exiting script.');
return 0; // Exit if group is not found
}
// Step 2: Fetch assignment group change logs from sys_audit
var audit = new GlideRecord('sys_audit');
audit.addQuery('documentkey', task.sys_id);
audit.addQuery('fieldname', 'assignment_group');
audit.orderBy('sys_created_on');
audit.query();
while (audit.next()) {
var newValue = audit.getValue('newvalue');
var currentTime = new GlideDateTime(audit.sys_created_on).getNumericValue();
gs.info('Audit Record: assignment_group changed to ' + newValue + ' at ' + audit.sys_created_on);
if (newValue === prepGroupSysId) {
if (previousTime) {
totalDuration += (currentTime - previousTime) / 1000; // Convert to seconds
gs.info('Adding duration: ' + (currentTime - previousTime) / 1000 + ' seconds');
}
previousTime = currentTime;
}
}
// Step 3: If still in Prep Group, add duration from last change to now
if (task.assignment_group.getValue() === prepGroupSysId && previousTime) {
var now = new GlideDateTime().getNumericValue();
totalDuration += (now - previousTime) / 1000;
gs.info('Adding ongoing duration: ' + (now - previousTime) / 1000 + ' seconds');
}
gs.info('Final Duration Calculated: ' + totalDuration + ' seconds');
return totalDuration;
})(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 02:52 AM
It's not working please only suggest if it's working for you i have tried all your suggestions nothing is working.

- 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-04-2025 04:51 AM
Thank you so much it's working as expected