Calculating how much time elapsed on each state ON INCIDENT

Abishek1998
Tera Contributor

Hi all,

So incident form goes to various states like inprogress, hold etc., So i want to calculate the time spent on each states.

 

I have tried it with business rule but didn't workout.

 

 

2 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Abishek1998 

 

Can you share what you have tried?

You can create two custom fields:

 

Create Fields:

  • Add a field like u_state_entry_time (Date/Time type) to the incident table to store when the state changed.
  • Add another field u_state_duration (Duration type) to store how long the incident was in that particular state.

And Write before BR

Condition: current.state.changes()

 

var now = new GlideDateTime();
var stateEntryTime = current.u_state_entry_time;


if (!stateEntryTime.nil()) {
var durationInPreviousState = gs.dateDiff(now, stateEntryTime, true); 
current.u_state_duration = durationInPreviousState; 
}current.u_state_entry_time = now;

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

Robbie
Kilo Patron
Kilo Patron

Hi @Abishek1998,

 

I'm interested as to why you have gone with adding 2 extra fields as per Ravi's suggestion? If the Incident has 5 or 6 states? How are you storing the time spend for each state? For example from New to In Progress, or In Progress to Resolved etc?

Do you overwrite the existing value? Have you created a field for each state? What happens then the ticket is on Hold or ever go back for example the Resolution is rejected?

 

As stated, metrics are available Out Of the Box (OOB) for you and captures every state duration? What is the user and reporting requirement here that I'm missing?

I'm just trying to make sure you implement the correct solution, and where possible follow best practice.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

View solution in original post

5 REPLIES 5

So can you please guide me in step by step process? of what to do ?