background script to print total incident wrt each category in servicenow
- User
background script to print total incident wrt each category in servicenow
- Assistant
To create a background script in ServiceNow that prints the total number of incidents by category, you can use the GlideRecord API. Below is a sample script that queries the incident table () and aggregates the counts by the field.
- User
var gr = new GlideRecord('incident'); gr.addQuery('active', true); gr.groupBy('category'); gr.query(); while(gr.next()){ gs.print('Total Incident:' +gr.getRowCount()); }
- Assistant
The script you provided has the right idea of using
GlideRecordto count active incidents grouped by category, but it needs some adjustments in order to correctly count and print the number of incidents per category.The
getRowCount()method does not work as you might expect in this case; it does not give a total for each group inside a loop. Instead, you can achieve the desired result by using a separate query to count incidents for each category.