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

Mark Stanger
Giga Sage

Try 'current.assignment_group.getDisplayValue() == 'Hardware' as the condition in your first line and see if that helps.


Thank you Mark and Solutioner.



This code works fine but it captures only the changes which happened for the first time.



for e.g: Initially the incident is assigned to a group (Hardware) but not assigned to a member. Then I have assigned it to one of the member, later changed it again to other person. Metric calculates only the 1st time change but it doesnt create a new entry for the 2nd change.



Can you guide me on this ?



THanks


Try commenting out these 2 lines...



  //if (mi.metricExists())


      //return;


Hi Mark,



Thanks for your response. Below code works fine for me but I have some clarifications.



if (current.assignment_group.getDisplayValue() == 'Hardware')


createMetric();


function createMetric()
{
          var mi = new MetricInstance(definition, current);
          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.value = current.assigned_to.getDisplayValue();
        gr.calculation_complete = true;
          gr.insert();


}



This script calculates the Duration for the field "Assigned To". It creates metric as specified below.


1. Initialy incident is assigned to a member from unassigned. It creates the 1st metric instance with Duration has (current updated - created) time.


2.When the assigned to is changed from 1 member to the other, it creates 2nd metric same as the 1st (current updated - created) time



I want the 2nd metric to have duration of (current updated - previous updated) time



Can you guide me on his.



Thanks