- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 01:12 PM
Get all the incidents, count of all the incident states like how many new and work in progress and on hold and on resolved and on closed???
Please need help
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 08:51 PM
you can achieve this by using the GlideAggregate API to query the incident table and group the results by the incident state.
// Create a new GlideAggregate query on the incident table
var ga = new GlideAggregate('incident');
// Add a query to group by incident state
ga.addQuery('state', 'IN', ['1', '2', '3', '6', '7']); // Adjust state values as per your instance
// Add a count aggregation to get the number of incidents in each state
ga.addAggregate('COUNT');
// Execute the query
ga.query();
// Initialize variables to store counts for each state
var newStateCount = 0;
var workInProgressCount = 0;
var onHoldCount = 0;
var resolvedCount = 0;
var closedCount = 0;
// Iterate through the results and populate counts for each state
while (ga.next()) {
var state = ga.getValue('state');
var count = parseInt(ga.getAggregate('COUNT'));
switch (state) {
case '1':
newStateCount = count;
break;
case '2':
workInProgressCount = count;
break;
case '3':
onHoldCount = count;
break;
case '6':
resolvedCount = count;
break;
case '7':
closedCount = count;
break;
default:
break;
}
}
// Now you can use the counts as needed
gs.info('New: ' + newStateCount);
gs.info('Work in Progress: ' + workInProgressCount);
gs.info('On Hold: ' + onHoldCount);
gs.info('Resolved: ' + resolvedCount);
gs.info('Closed: ' + closedCount);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 09:04 PM
Hi @purnendu check below code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 09:33 PM
Hi @purnendu ,
Use below code, Its working fine I tested it in my PDI.
// Create a GlideAggregate object to group incidents by state
var grIncident = new GlideAggregate('incident');
grIncident.groupBy('state'); // Group by the 'state' field
// Add a COUNT aggregate to count the number of incidents in each state
grIncident.addAggregate('COUNT');
// Execute the query
grIncident.query();
// Iterate through the results
while (grIncident.next()) {
// Get the display value of the 'state' field
var state = grIncident.getDisplayValue('state');
// Get the count of incidents in this state
var count = grIncident.getAggregate('COUNT');
// Print the result
gs.print('State: ' + state + ', Total Count: ' + count);
}
If my response helped you, please click on "Accept as solution" and mark it as helpful.
Thanks
Suraj!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 08:51 PM
you can achieve this by using the GlideAggregate API to query the incident table and group the results by the incident state.
// Create a new GlideAggregate query on the incident table
var ga = new GlideAggregate('incident');
// Add a query to group by incident state
ga.addQuery('state', 'IN', ['1', '2', '3', '6', '7']); // Adjust state values as per your instance
// Add a count aggregation to get the number of incidents in each state
ga.addAggregate('COUNT');
// Execute the query
ga.query();
// Initialize variables to store counts for each state
var newStateCount = 0;
var workInProgressCount = 0;
var onHoldCount = 0;
var resolvedCount = 0;
var closedCount = 0;
// Iterate through the results and populate counts for each state
while (ga.next()) {
var state = ga.getValue('state');
var count = parseInt(ga.getAggregate('COUNT'));
switch (state) {
case '1':
newStateCount = count;
break;
case '2':
workInProgressCount = count;
break;
case '3':
onHoldCount = count;
break;
case '6':
resolvedCount = count;
break;
case '7':
closedCount = count;
break;
default:
break;
}
}
// Now you can use the counts as needed
gs.info('New: ' + newStateCount);
gs.info('Work in Progress: ' + workInProgressCount);
gs.info('On Hold: ' + onHoldCount);
gs.info('Resolved: ' + resolvedCount);
gs.info('Closed: ' + closedCount);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 09:04 PM
Hi @purnendu check below code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 09:33 PM
Hi @purnendu ,
Use below code, Its working fine I tested it in my PDI.
// Create a GlideAggregate object to group incidents by state
var grIncident = new GlideAggregate('incident');
grIncident.groupBy('state'); // Group by the 'state' field
// Add a COUNT aggregate to count the number of incidents in each state
grIncident.addAggregate('COUNT');
// Execute the query
grIncident.query();
// Iterate through the results
while (grIncident.next()) {
// Get the display value of the 'state' field
var state = grIncident.getDisplayValue('state');
// Get the count of incidents in this state
var count = grIncident.getAggregate('COUNT');
// Print the result
gs.print('State: ' + state + ', Total Count: ' + count);
}
If my response helped you, please click on "Accept as solution" and mark it as helpful.
Thanks
Suraj!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2024 03:17 AM
Thank You Suraj. It works @surajchacherkar