to count all the different state in the incident table

purnendu
Tera Contributor

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

3 ACCEPTED SOLUTIONS

Maddysunil
Kilo Sage

@purnendu 

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

View solution in original post

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @purnendu check below code

var ga = new GlideAggregate('incident');
ga.groupBy('state');
ga.addAggregate('COUNT');
ga.query();

while (ga.next()) {
var state = ga.getDisplayValue('state');
var count = ga.getAggregate('COUNT');
gs.print('State: ' + state + ', Count: ' + count);
}
Screenshot 2024-06-10 at 9.33.11 AM.png

View solution in original post

surajchacherkar
Mega Guru

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);
}

 

surajchacherkar_1-1717993914307.png

 

 

If my response helped you, please click on "Accept as solution" and mark it as helpful.


Thanks

Suraj!

 

View solution in original post

6 REPLIES 6

Maddysunil
Kilo Sage

@purnendu 

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

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @purnendu check below code

var ga = new GlideAggregate('incident');
ga.groupBy('state');
ga.addAggregate('COUNT');
ga.query();

while (ga.next()) {
var state = ga.getDisplayValue('state');
var count = ga.getAggregate('COUNT');
gs.print('State: ' + state + ', Count: ' + count);
}
Screenshot 2024-06-10 at 9.33.11 AM.png

surajchacherkar
Mega Guru

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);
}

 

surajchacherkar_1-1717993914307.png

 

 

If my response helped you, please click on "Accept as solution" and mark it as helpful.


Thanks

Suraj!

 

Thank You Suraj. It works @surajchacherkar