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.

Incident metric does not capture latest 'updated_by' user

kim-lindgren
Kilo Sage

I have a requirement to create an incident metric whenever there is an insert or update to an incident record. The userID in the Updated by field is saved as the value for the metric, allowing you to see who has made an update to an incident and how many times they have updated it. It only runs for itil users and for certain assignment groups.

 

I have implemented this using an idea from a blog which I am supposedly not allowed to link, so there are my scripts for clarity:

BR

 

(function onAfter(current, previous) {
   gs.eventQueue('metric.update', current, '[sys_updated_by]', 1, 'metric_update');
})(current, previous);

 

 

Metric Definition Script

 

createMetric(current.sys_updated_by.toString());

function createMetric(value) {
	var mi = new MetricInstance(definition, current);
	var miGr = mi.getNewRecord();
	miGr.field_value = value;
	miGr.calculation_complete = true;
	miGr.insert();
}

 

 

This works fine the first time for any incident, whether you create a new one or start playing with an existing one created by someone else. But any subsequent time a metric instance is created this way, it registers the same Updated by value as for the first Updated by metric instance of that incident!

 

For example: If Fred Luddy created an incident, then Beth Anglin assigned it to one of the target assignment groups and made three updates to the incident, those three updates will all create a metric instance with "beth.anglin" as value. But then if another itil user comes along and makes two additional updates - two more metric instances with the value "beth.anglin" are created.

 

What could be the reason for this? Note that I have already set my After BR to order 100,000 just to be sure.

 

Regards,

Kim

0 REPLIES 0