Metric_instance records are being duplicated

Ryan153
Giga Expert

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

}

10 REPLIES 10

Michael Fry1
Kilo Patron

While your script looks ok, I see nothing about worknotes. I'm also unclear why your using a gliderecord to get the user sys_id?



This Metric should be running on every ticket where the assignment group is IT Service Desk TMSS


Thanks for the reply Michael,


In response to the gliderecord, I was going to also include the User's name which is why I chose the code I did, but after discovering you can only update the 'value' field if you don't update the 'field_value' I decided the sys_id of the User was most valuable since I had to pick one.   I am guessing there are several ways I could get the sys_id of the User updating the record.   Suggestions are welcome as I am a novice at creating these metrics.



Reading the 'I see nothing about worknotes', the intent of the metric is not to capture the work notes themselves, just the who for the User who updated the Incident.



In the   screen shot you can see the Work Notes are defined in the Field option, make sense?9-5-2017 8-49-21 AM.jpg


Ok, sorry, missed the work notes field. For the value, can't you just use something like current.sys_updated_by?


current.sys_updated_by is already in the script logic and returns a User Name and not a sys_id.



Existing Code snippet:


...


var uname = current.sys_updated_by;


var ugr = new GlideRecord('sys_user');


ugr.get('user_name', uname);


...