- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
It is the # of updates done to the record.
Every number corresponds to a certain save state of the record. Script actions (how metrics are handled by the platform) query the record in its current state at the time of the event processing job.
Consider you have 10 updates on the same record in rapid succession and all the "metric.update" events are processed at the same time. The event does not relay the information of what the state of the record was at the time of the event being fired. So all your metrics would have access to the same sys_updated_by value which is the last update which might not even be an update in the mentioned 10 updates.
If you look at the Metric Update script action that handles the metric.update event you can see that it does a rollback to the event.parm2'th update.
var updateCount = new String(event.parm2);
...
rollback(current, updateCount);
...
function rollback(gr, updateCount) {
var r = new GlideRecordRollback();
r.toVersion(gr, updateCount);
}
You can test it in a background script:
var current = new GlideRecord("incident");
if (current.get("<sysid>")) {
var updateCount = parseInt(current.getValue("sys_mod_count"));
while (updateCount) {
var r = new GlideRecordRollback();
r.toVersion(current, updateCount);
gs.eventQueue('metric.update', current, '[sys_updated_by]', updateCount, 'metric_update');
updateCount--;
gs.info(gs.getMessage("Update #{0} with value: {1}", [updateCount, current.getValue("sys_updated_by")]));
}
}
Thus update your method call to this to pass the correct version instead of 1 and it should work.
gs.eventQueue('metric.update', current, '[sys_updated_by]', current.getValue("sys_mod_count"), 'metric_update');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This worked fine for me in a pdi, I would review the definition field value and also add the sys_mod_count value into parm2 as that is how the event is queued oob.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Sorry, what is the sys_mod_count value and how do I add it to the second parameter?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
It is the # of updates done to the record.
Every number corresponds to a certain save state of the record. Script actions (how metrics are handled by the platform) query the record in its current state at the time of the event processing job.
Consider you have 10 updates on the same record in rapid succession and all the "metric.update" events are processed at the same time. The event does not relay the information of what the state of the record was at the time of the event being fired. So all your metrics would have access to the same sys_updated_by value which is the last update which might not even be an update in the mentioned 10 updates.
If you look at the Metric Update script action that handles the metric.update event you can see that it does a rollback to the event.parm2'th update.
var updateCount = new String(event.parm2);
...
rollback(current, updateCount);
...
function rollback(gr, updateCount) {
var r = new GlideRecordRollback();
r.toVersion(gr, updateCount);
}
You can test it in a background script:
var current = new GlideRecord("incident");
if (current.get("<sysid>")) {
var updateCount = parseInt(current.getValue("sys_mod_count"));
while (updateCount) {
var r = new GlideRecordRollback();
r.toVersion(current, updateCount);
gs.eventQueue('metric.update', current, '[sys_updated_by]', updateCount, 'metric_update');
updateCount--;
gs.info(gs.getMessage("Update #{0} with value: {1}", [updateCount, current.getValue("sys_updated_by")]));
}
}
Thus update your method call to this to pass the correct version instead of 1 and it should work.
gs.eventQueue('metric.update', current, '[sys_updated_by]', current.getValue("sys_mod_count"), 'metric_update');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Very good explanation, thanks! And it works.