Metric to get created_date of previous record generated for same incident

Kumar116
Tera Contributor

Looking for metric script where i can get difference of created_date of two records generated for same incident. For example, metric triggers each time there is any comment mentioned on ticket. i need difference between creation of both records generated. Already have created below script but its showing blank start date.

function createMetric()
{
var prev_start_date;
var grMI = new GlideRecord('metric_instance');
grMI.addEncodedQuery("definition="+ current.definition);
grMI.orderByDesc('sys_created_on');
grMI.setLimit(1);
grMI.query();
if (grMI.next())
{
prev_start_date=grMI.getValue('sys_created_on');
}

var mi = new MetricInstance(definition, current);
var gr = mi.getNewRecord();
gr.start = prev_start_date;
gr.end = current.sys_created_on;
//gr.end = gs.nowDateTime();
gr.calculation_complete = true;
gr.insert();

2 REPLIES 2

Community Alums
Not applicable

Hi @Kumar116 ,

To get the created date of the previous record generated for the same incident in ServiceNow, you can use the following metric:

 

javascript: (function () {
  var gr = new GlideRecord('table_name');
  gr.addQuery('incident', current.incident);
  gr.orderByDesc('sys_created_on');
  gr.query();
  if (gr.next()) {
    current.prev_created_date = gr.getValue('sys_created_on');
  }
})();

 

This metric retrieves all the records from the specified table that have the same incident as the current record, orders them by descending order of creation date, and retrieves the first record, which is the previous record generated for the same incident. It then sets the prev_created_date field on the current record to the created date of the previous record.

Replace table_name with the name of the table containing the records, and incident with the name of the incident field on the table. You can also modify the prev_created_date field to match the field name in your table where you want to store the previous record's created date.

Note that this metric assumes that the previous record was created on the same table and has the same incident value as the current record. You may need to modify the metric to suit your specific use case.

 

Hello sandeep,

 

I have to find the reassignments counts of the incidents. Like first they werr assignd to me then i assigned to other person. So how can i pull the report out from service now ? 

Actually i am new to this plateform and i tried searching incident metric was unable to. 

Can you please tell me which way to go.

 

Thanks.