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

Shubham_Jain
Mega Sage
Mega Sage

@SM123 

 

(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


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!

@SM123 

(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


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

 

@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