Incidents count
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 03:37 AM
I need a Incident state change records count through script.
Count the state New to Inprogess incident records count.
Cold you please help any one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 06:01 AM
HI @vinod6,
What's your design for this , have you create a new column in incident table for this count.
you can write a business rule [ after : update ] condition on state column and increment the count value and if you are looking for all state change recording then write metrics on incident table and get the report on that data.
Query -> How many times you are expecting this state change from "New" to "In Progress" for any incident, it's only ONE time, if it's that then why you need to calculate it.
Please review the requirement and explain in details.
-Thanks,
AshishKMishra
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 06:35 AM
what's the business requirement for this?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 06:45 AM
@vinod6 Try the following script and see it it helps.
var list = new GlideRecord("sys_history_set");
list.addEncodedQuery("tableSTARTSWITHincident^id=e0477ee847faf11092c98021336d43fb"); //id is sys_id of the incident
var stateCount=0;
list.query();
if (list.next()){
var glideAgg = new GlideAggregate('sys_history_line');
glideAgg.addEncodedQuery('set.id='+list.id+'^label=State^old=Resolved^new=Closed');
gs.print('set.id='+list.id+'^label=State^old=New^new=In Progress');
glideAgg.addAggregate('COUNT');
glideAgg.query();
if(glideAgg.next()){
stateCount=glideAgg.getAggregate('COUNT');
}
}
gs.info(stateCount);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 07:34 AM
HI @vinod6 ,
I trust you are doing great.
Below is a basic example of how such a script might look:
var count = 0;
var incidentGR = new GlideRecord('incident'); // Create a new GlideRecord instance for the incident table
incidentGR.addQuery('state', '1'); // Filter for incidents that are in the 'New' state
incidentGR.query(); // Query the table
while (incidentGR.next()) {
// Check if the current record has transitioned to 'In Progress'
if (incidentGR.state.changesTo('2')) {
count++;
}
}
gs.print('Number of incidents that changed from New to In Progress: ' + count);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi