Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Script for printing count of incidents per assignment group

Manikandan2
Kilo Expert

Hi,

Am new to scripting and i want to print the count of incidents per assignment group. 

Any scripts available? 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Manikandan,

you can use reporting for this as well.

sample code below:

var gr = new GlideRecord('incident');

gr.addQuery('assignment_group!=NULL');

gr.query();
while(gr.next()){

var gr1 = new GlideRecord('incident');
gr1.addQuery('assignment_group', gr.assignment_group);
gr1.query();
var rowCount = gr1.getRowCount();

gs.print("Incidents for Group: " + gr.assignment_group.getDisplayValue() + " are " + rowCount);

}

the above code can be optimized using GlideAggregate

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Manikandan,

you can use reporting for this as well.

sample code below:

var gr = new GlideRecord('incident');

gr.addQuery('assignment_group!=NULL');

gr.query();
while(gr.next()){

var gr1 = new GlideRecord('incident');
gr1.addQuery('assignment_group', gr.assignment_group);
gr1.query();
var rowCount = gr1.getRowCount();

gs.print("Incidents for Group: " + gr.assignment_group.getDisplayValue() + " are " + rowCount);

}

the above code can be optimized using GlideAggregate

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you for the code Ankur

Nabin Agarwal
Tera Contributor

Hi Manikandan,

 

Try this piece of code below-

var gr = new GlideAggregate('incident');
gr.groupBy('assignment_group');
gr.query();
while(gr.next()){
   gs.print(gr.assignment_group.name);
   var gr1 = new GlideRecord('incident');
   gr1.addQuery('assignment_group',gr.assignment_group);
   gr1.query();
   if(gr1.next()){
        gs.print(gr1.getRowCount());
   }
}