Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Metric Definition Script to capture Assigned to and Assigned Group

kannann_
Kilo Expert

Hello All,

I have a field value Metric Definition which captures Assigned to value. I would also like to capture the Assigned Group in the value field when the Assigned to field changes. How do I do that? Please suggest.

1 ACCEPTED SOLUTION

11 REPLIES 11

Michael, that is what I am trying to use right now. Below is the code inside the Metric Definition.


But this does not seems to work



if(current.assigned_to.changes())


{


  var metricSysID = '98053f806f070e0074e990754b3ee4a3';


  var instanceRecord= new GlideRecord('metric_instance');


  instanceRecord.addQuery('id',current.sys_id);


  instanceRecord.addQuery('definition',metricSysID);


  instanceRecord.addQuery('calculation_complete','false');


  instanceRecord.query();


 


 


  if(!instanceRecord.next())


  {


  insertMetrics();


  }


  else


  {


  instanceRecord.end =gs.nowDateTime();


  instanceRecord.calculation_complete = true;


  instanceRecord.update();


  insertMetrics();


  }


}




function insertMetrics()


{



      var instanceRecord= new GlideRecord('metric_instance');


      var metricSysID = '98053f806f070e0074e990754b3ee4a3'; //replace the sys id of the metric definition


      instanceRecord.initialize();


      instanceRecord.definition = metricSysID;


      instanceRecord.start = gs.nowDateTime();


      instanceRecord.id = current.sys_id;


      instanceRecord.value = current.assigned_to+ ' '+current.assignment_group; //replace the fieldname here


      instanceRecord.calculation_complete = false;


      instanceRecord.insert();



}


Hi @Michael Fry 

 

Can you please help me with below query

We need to calucate knowledge Article Approval duration only when the state is in review state

calculate the total time in days and hours that a knowledge article version is in the state ''review''

 

Thanks,

Anusha

kannann_
Kilo Expert

Hey all,



I actually got this to be working by removing the current.assigned_to.changes().


Now the issue is, I need the metrics to create new record only if the assigned group is a particular group.


I tried adding if(current.assignment_group.getDisplayValue() == 'My assigned group' || current.assignment_group.getDisplayValue() == 'My assigned group2' ) but that again stopped the metrics from working.


What am I missing?


You can only use current in Metric, not .changes() unfortunately. Did you try by sys_id? current.assignment_group == 'sys_id here';


I tried using sys_id   if(current.assignment_group == 'sys_id') as well Michael but did not work