Usecase: Calculate the average time taken to resolve requests in a specific category.

Kongaleti Navee
Tera Contributor

Achieve the above given script in the below given format

 

 

Usecase: Calculate the total duration of a Incident .

   Description: Create a Script Include that calculates the total duration of a Incident based on its start and end time fields.

 

 

Script Include:-

 

var CalculateDuration = Class.create();

CalculateDuration.prototype = {

    initialize: function() {},

 

    type: 'CalculateDuration',

   calculateDurationDate:function(dateof){

gs.info('newdate of'+dateof.sys_created_on)

gs.info('olddate of'+dateof.u_duration)

gs.info('olddate of'+gs.dateDiff(dateof.sys_created_on,dateof.u_duration,true))

 

 

   }

   

};

 

Background script:-

 

var gr = new GlideRecord('incident');

gr.addQuery();

gr.query();

if(gr.next()){

gs.info(gr.number);

new CalculateDuration().calculateDurationDate(gr);

}

 

Result:-

 

*** Script: INC0010018

*** Script: newdate of2023-12-27 18:40:59

*** Script: olddate of2023-12-24 05:42:29

*** Script: olddate of-305910

2 REPLIES 2

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @Kongaleti Navee , 

 

There is another approch as well. That we can consider. 


To calculate the average time taken to resolve requests in a specific category, you can create a Metric Definition in ServiceNow.

Here are the steps:

1. Navigate to "System Definition > Metric Definitions" in ServiceNow.

2. Click on "New" to create a new Metric Definition.

3. In the "Table" field, select the table you want to collect metrics from. For instance, if you're tracking Request resolution time, you'd select the "Request" table.

4. In the "Name" field, give your Metric Definition a name, such as "Average Resolution Time".

5. In the "Type" field, select "Duration".

6. In the "Start condition" field, define when the metric should start tracking. For instance, you might start tracking when an Request's state changes to "New or In Progress ".

7. In the "Stop condition" field, define when the metric should stop tracking. For instance, you might stop tracking when an Request's state changes to "Resolved" or "Closed".

8. In the "Script" field, you can add a script to further refine your metric. For instance, you might want to only track Requests in a specific category.

9. Click on "Submit" to save your Metric Definition. Here's a sample script for the "Script" field that only tracks incidents in a specific category:

if (current.category == 'your_specific_category') { 
return true; 
} else 
{ 
return false;
}

 Once you've created your Metric Definition, ServiceNow will start collecting data. You can then use this data to calculate the average resolution time.

Kongaleti Navee
Tera Contributor

thank you siddesh, but in the interview if you want to show the result in the background script how we can achieve the above scenario by using script include if they specify ,so that i am trying to ask this question