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'd encourage you to try the method I posted below.   Let the metric system handle populating the records.   However, if you feel you need to create it with a script, you definitely need more complex logic.   You'll need the line you commented out to detect if a metric exists, if it does, you'll need to close that record setting the end to the current.sys_updated_on.   Then in your code to create the next metric, leave the end and duration empty, and set calculation_complete to false.   The start would also then be the current.sys_updated_on.



You'll also need to add some logic so that new instance records aren't created when the incident is closed, or you'll have extra records floating around that don't provide any value.


solutioningnow
Giga Guru

Hi Mathews,



In metrics definition current points to the incident record to check for any specific assignment group should be writing below condition,



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


  createMetric();




Try to make this changes, let me know the results.




Please mark answer as correct/helpful, if it was really helpful



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


http://www.solutioningnow.com/


Solutioner, isn't that exactly what I just said?


Yeah mark,



True ! Actually i drafted it and was on another thread at the same time, while i came back i didn't refreshed and posted .



Anyways, Mathews Praveen both are same , Mark was first on it.


Chris M3
Tera Guru

You actually don't need to use a script calculation for this.


Use a Field Duration on the Assigned to Field


In the script box use the below code.



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


  mi.endDuration();


  return false;


}



What this does is gets triggered on any change to the assigned to field in the Incident.   If the assignment group is not 'Hardware', then it ends any current active metrics for that definition/incident (Maybe the previous assignment group was Hardware, so there's a currently active metric instance), and doesn't create a new one.   Otherwise it processes normally.