Creation of Metric with Script Calculation ?

Mathews1
Tera Contributor

Hi Guys,

 

I would like to create a Metric to capture the "Assigned To" field changes. My requirement is to capture the field changes only for a specific group. When the "Assigned To" of an incident changes for the particular group, then it should be captured in the Metric table.

 

Script which I have used:

 

if (current.incident.assignment_group == "Hardware")
  createMetric();

function createMetric()
{
var mi = new MetricInstance(definition, current);
  if (mi.metricExists())
  return;

    var gr = mi.getNewRecord();
  gr.start = current.sys_created_on;
  gr.end = current.sys_updated_on;

gr.duration = gs.dateDiff(gr.start, gr.end);
gr.calculation_complete = true;
  gr.insert();

}

 

But the Metric definition is not having / capturing any field change values. Please guide me on thi.

 

Thanks

19 REPLIES 19

I think the issue might be that the assigned_to field is not actually changing.   The easiest way to correct for this is to trigger a metric event in a new business rule, with the 'assigned_to' field as being changed.   You'll have to have some conditions on group field, looking at previous and current assigned_to, and trigger an event sending assigned_to as the changed field(look at the current metric events business rule for the format).   Unfortunately I'm omw out of town, but hopefully this points you in the right direction.


An on insert business rule on the incident table with the condition current.assigned_to.isNil()



gs.eventQueue('metric.update', current, '[assigned_to]', current.sys_mod_count, 'metric_update');



The reason a record wasn't getting created is that the assigned_to field wasn't a field that changed.   The above code will tell the metric events processor to start a metric for assigned_to with the blank value.


Hi Mathews,



use the condition 'current.assinged_to.changes();' so that it will take care of your empty "assigned to" field as well.


Hope this should work.


pyalcari
Tera Contributor

Hi


i have the same issue. Metric instance value is starting from zero. I want it to be starts from 1.



please suggest.



Thanks


Deepansh Jain
Kilo Contributor

Can someone answer this metric related question . Please

 

https://community.servicenow.com/community?id=community_question&sys_id=92bd9c38db5b18903daa1ea66896191d