Metric_instance records are being duplicated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 10:19 AM
I created a metric (code below) which is using:
Table:Incident
Field: Work notes
Type:Script calculation
The goal is to create a metric instance when a User adds a work note. But I am seeing duplicate metric records (different sys_id but same field values) . It also appears that this only happens when you hit Post instead of hitting Saving/Updating. I have also seen this behavior when I attempt to add a Work note to an Incident.list instead of actually being in the Incident form
// variables available
// current: GlideRecord - target incident
// definition: GlideRecord - (this row)
createMetric();
function createMetric() {
// Only process for IT Service Desk TMSS Team
if (current.assignment_group.getDisplayValue()!='IT Service Desk TMSS' )
return;
var mi = new MetricInstance(definition, current);
var gr = mi.getNewRecord();
var uname = current.sys_updated_by;
var ugr = new GlideRecord('sys_user');
ugr.get('user_name', uname);
/*Note: You can only update 'value' field if you don't update 'field_value'. There is a OOB Business rule on Metric Instance which will override 'value' field before insert, if you change 'field_value'.*/
gr.value = ugr.sys_id;
gr.field = definition.field;
gr.start = current.sys_updated_on;
gr.end = current.sys_updated_on;
gr.calculation_complete = true;
gr.insert();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2017 07:59 AM
current.sys_updated_by will contain an email address. Try querying on email and make sure to add ugr.query();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2017 05:54 PM
What happens if you replace:
var mi = new MetricInstance(definition, current);
with this:
var mi = new MetricInstance(definition, current);
if (mi.metricExists())
return;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2017 09:02 PM
The metricExists() function in the MetricInstance script include checks to see if the instance that it is about to create already exists (based on sys_id) - Ryan's issue is that there are metric instances with different sys_ids being created, with the same content, so it would pass through metricExists() ok

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2017 06:59 PM
Regarding the duplicate metrics being created - I have just set one up in my dev instance (which is Jakarta) and can't reproduce the issue - I only get one metric instance created whether I update the work notes directly on the form and Save, or directly on the form and click Post, or from the list.
First thing I would check is whether there is something else re-saving/re-posting the work note - if you check the audit history this might tell you if there are multiple entries being saved to the journal - in the incident context menu go to History -> List, then scroll down to Work Notes, do you see duplicate entries for the same set of work notes? This is what I see from my testing, one per entry, do you see the same?:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2017 04:45 AM
Thanks Bonnie I think you are correct, there is probably some work flow logic interfering with the metric being written. I have ask our development team to take a look for me since I do not have permissions to do a deep dive into the code.
Appreciate you taking the time to test and respond !