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 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();
		
    }

 

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

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))

KieranAnson_0-1741085958882.png

 

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

 

 

View solution in original post

18 REPLIES 18

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

@SM123 

(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


@Shubham_Jain ,

No it's not working Shubham please let me know if it's working for you

@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


@Shubham_Jain ,

Yes this background script is printing sysid of Prep Group. but i need help in metrics definition