Can a metric_instance Value field be scripted?

Ryan153
Giga Expert

Purpose:  

We have a team that wants to measure how many times they are touching an Incident and who did the touch, they define a touch as adding a Work Note from their team. Because they do NOT assign Incidents to individuals we cannot use assignment metrics.

Problem:

I have created a metric with the purpose of having the Work Note trigger the creating of the metric.   My desire is to have the metric records contain the field_value = [Name of User] and value=[sys_id of User]. The metric is created as I expected it to, however no matter what I try to put in the value field of the metric_instance record, I always get the contents of the work note and not the sys_id.   The field_value is correctly populating the user name. How can I populate the metric's value field with what I need?

Script details

TABLE:Incident

FIELD:Work notes

TYPE: Script calculation

// variables available

// current: GlideRecord -   target incident

// definition: GlideRecord -   (this row)

createMetric();

function createMetric() {

     

      // Only process if TMSS group is assigned to Incident

      if (current.assignment_group.getDisplayValue()!='IT Service Desk TMSS' )   //using Name for Dev only

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

      gr.field_value = ugr.name;

      gr.field = definition.field;

      // nothing is working here //gr.value = 'A PUPPY!';// ugr.get('sys_id'); //ugr.sys_id

      gr.start = current.sys_updated_on;

      gr.end = current.sys_updated_on;

      gr.calculation_complete = true;

      gr.insert();

}

1 ACCEPTED SOLUTION

nagaraniracharl
ServiceNow Employee
ServiceNow Employee

Hi Ryan,


You will be able to 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'.



Try commenting the below line:


gr.field_value = ugr.name;


View solution in original post

3 REPLIES 3

nagaraniracharl
ServiceNow Employee
ServiceNow Employee

Hi Ryan,


You will be able to 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'.



Try commenting the below line:


gr.field_value = ugr.name;


Thanks for the reply Nagarani. I did comment out the 'field_name' assignment and used the sys_id of the user record which worked fine. I guess if I want additional user attributes I now have the sys_id to take me there.



Thanks for the knowledge!


Deepansh Jain
Kilo Contributor

@nagaraniracharla 

I have asked a similar question , Would you be to create a script ??

https://community.servicenow.com/community?id=community_question&sys_id=09714a971b3e54d08672ea89bd4b...