Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need Help in Script

Mark Wood
Tera Contributor

Hello Experts,

I want to print the incident category with the incident number I have written below script 

but is not working. please guide me on this.

thank you.

var incidentGR = new GlideAggregate('incident');
incidentGR.addQuery('active', true);
incidentGR.groupBy('category');
incidentGR.addAggregate('COUNT', 'number');
incidentGR.query();
while (incidentGR.next()) {
  var category = incidentGR.category.getDisplayValue();
  var incidentCount = incidentGR.getAggregate('COUNT', 'number');
 var number=incidentGR.number;
  gs.info('Category: ' + category + ' - Incident Count: ' + incidentCount);
  gs.print('number'+number);
}

 

 

1 ACCEPTED SOLUTION

VarunS
Kilo Sage

@Mark Wood Try this

 

var incidentGR = new GlideAggregate('incident');
incidentGR.addQuery('active', true);
incidentGR.groupBy('category');
incidentGR.addAggregate('COUNT', 'number');
incidentGR.query();
while (incidentGR.next()) {
  var category = incidentGR.getValue('category'); // Get the category value directly
  var incidentCount = incidentGR.getAggregate('COUNT', 'number');
  
  // Query the incident records for numbers within this category
  var incidentNumbers = new GlideRecord('incident');
  incidentNumbers.addQuery('category', category);
  incidentNumbers.query();
  
  // Print each incident number along with the category and count
  while (incidentNumbers.next()) {
    gs.info('Category: ' + category + ' - Incident Count: ' + incidentCount);
    gs.print('Incident Number: ' + incidentNumbers.number);
  }
}

 

View solution in original post

3 REPLIES 3

Ranjit Nimbalka
Mega Sage

Hi @Mark Wood ,

 

Please try below code:-

 

var incidentGR = new GlideAggregate('incident');
incidentGR.addQuery('active', true);
incidentGR.groupBy('category');
incidentGR.addAggregate('COUNT');
incidentGR.query();
while (incidentGR.next()) {
  var category = incidentGR.category.getDisplayValue();
  var incidentCount = incidentGR.getAggregate('COUNT');
 var number=incidentGR.number;
  gs.info('Category: ' + category + ' - Incident Count: ' + incidentCount);

}

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Ranjit

VarunS
Kilo Sage

@Mark Wood Try this

 

var incidentGR = new GlideAggregate('incident');
incidentGR.addQuery('active', true);
incidentGR.groupBy('category');
incidentGR.addAggregate('COUNT', 'number');
incidentGR.query();
while (incidentGR.next()) {
  var category = incidentGR.getValue('category'); // Get the category value directly
  var incidentCount = incidentGR.getAggregate('COUNT', 'number');
  
  // Query the incident records for numbers within this category
  var incidentNumbers = new GlideRecord('incident');
  incidentNumbers.addQuery('category', category);
  incidentNumbers.query();
  
  // Print each incident number along with the category and count
  while (incidentNumbers.next()) {
    gs.info('Category: ' + category + ' - Incident Count: ' + incidentCount);
    gs.print('Incident Number: ' + incidentNumbers.number);
  }
}

 

Sandeep Rajput
Tera Patron
Tera Patron

@Mark Wood Here is the script you should try.

var incidentGR = new GlideAggregate('incident');
incidentGR.addQuery('active', true);
incidentGR.groupBy('category');
incidentGR.addAggregate('COUNT');
incidentGR.query();
while (incidentGR.next()) {
  var incidentCount = incidentGR.getAggregate('COUNT');
  gs.info('Category: ' + incidentGR.category.getDisplayValue() + ' - Incident Count: ' + incidentCount);
}