- 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 12:34 AM
Hello @Shubham_Jain ,
I have replaced task.sysid manually to my Incident sysid still it's not working. anyhow it should be dynamic. It's not returning any values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 12:42 AM
(function calculateMetric(task, metric) {
var totalDuration = 0;
var previousTime = null;
// Dynamically fetch the sys_id of the "Prep Group"
var prepGroup = new GlideRecord('sys_user_group');
prepGroup.addQuery('name', 'Prep Group'); // Match by name
prepGroup.query();
if (!prepGroup.next()) {
return 0; // Exit if Prep Group is not found
}
var prepGroupSysId = prepGroup.getUniqueValue(); // Store the sys_id dynamically
// Query sys_audit for assignment group changes related to this task
var audit = new GlideRecord('sys_audit');
audit.addQuery('documentkey', task.sys_id);
audit.addQuery('fieldname', 'assignment_group');
audit.addQuery('newvalue', prepGroupSysId); // Filter only when assigned to "Prep Group"
audit.orderBy('sys_created_on'); // Sort in chronological order
audit.query();
while (audit.next()) {
var currentTime = new GlideDateTime(audit.sys_created_on).getNumericValue();
if (previousTime) {
totalDuration += (currentTime - previousTime) / 1000; // Convert milliseconds to seconds
}
previousTime = currentTime;
}
// If the task is currently assigned to "
✔️ 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:50 AM
No it's not working Shubham please let me know if it's working for you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 01:27 AM
@SM123 can you check with this
Verify "Prep Group" Exists:
Run this in a background script to check if "Prep Group" is found:
var grp = new GlideRecord('sys_user_group');
grp.addQuery('name', 'Prep Group'); // Ensure the name matches exactly
grp.query();
if (grp.next()) {
gs.info('Prep Group sys_id: ' + grp.getUniqueValue());
} else {
gs.info('Prep Group not found!');
}
✔️ 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 01:57 AM
Yes this background script is printing sysid of Prep Group. but i need help in metrics definition