- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 02:30 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 02:42 AM
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 03:32 AM - edited 09-09-2024 03:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 02:40 AM
Hi. It's already done by ServiceNow. Check the below link:
https://<instance>.service-now.com/nav_to.do?uri=metric_definition.do?sys_id=35f2b283c0a808ae000b7132cd0a4f55
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 02:42 AM
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 02:42 AM
Hi @Abishek1998,
No problem... this has been handled for you by way of Metrics.
Check out the "Incident State Duration" definition by typing "Metric Definition" in the Navigation menu.
You can the display the Metrics (if it's not already) by configuring the "Related Lists"
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 03:32 AM - edited 09-09-2024 03:50 AM
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