Query for all incidents with a category of ‘hardware’…log a count of records returned

Philip Conforzi
Tera Contributor

Hello everyone!

 

I have been working on a solution to this problem for a while, but have not been able to do it successfully.

 

I am trying to load the number of incidents with the category hardware that exist when I load the Incident table in ServiceNow.

 

I have attached the image of my script include below.

 

Do I need a UI policy from here? I don't think a client script will work in this case as I want the count to appear in a pop-up when the Incident table loads.

 

PhilipConforzi_0-1705095882763.png

 

 

 

1 ACCEPTED SOLUTION

ahefaz1
Mega Sage

@Philip Conforzi ,

Why not use a display business rule. Check below:

 

ahefaz1_0-1705100500191.png

 

 

 

 var gaIncident = new GlideAggregate('incident');
    gaIncident.addQuery('category', 'hardware');
    gaIncident.addAggregate('COUNT');
    gaIncident.query();

    if (gaIncident.next()) {
        var count = gaIncident.getAggregate('COUNT');
        gs.addErrorMessage('Number of incidents with category hardware: ' + count);
        gs.addInfoMessage('Number of incidents with category hardware: ' + count);
    }

 

 

You can modify the message type you want to show, and it will display whenever you open an incident record.

 

ahefaz1_0-1705100663406.png

 

 

Please accept solution OR mark helpful.

 

Thanks,

View solution in original post

3 REPLIES 3

ahefaz1
Mega Sage

@Philip Conforzi ,

Why not use a display business rule. Check below:

 

ahefaz1_0-1705100500191.png

 

 

 

 var gaIncident = new GlideAggregate('incident');
    gaIncident.addQuery('category', 'hardware');
    gaIncident.addAggregate('COUNT');
    gaIncident.query();

    if (gaIncident.next()) {
        var count = gaIncident.getAggregate('COUNT');
        gs.addErrorMessage('Number of incidents with category hardware: ' + count);
        gs.addInfoMessage('Number of incidents with category hardware: ' + count);
    }

 

 

You can modify the message type you want to show, and it will display whenever you open an incident record.

 

ahefaz1_0-1705100663406.png

 

 

Please accept solution OR mark helpful.

 

Thanks,

Thanks so much for you help! This really worked well for me, will be adding GlideAggregate to my developer toolbox!

Sandeep Rajput
Tera Patron
Tera Patron

@Philip Conforzi Please update your script as follows.

var gr = new GlideRecord('incident');
gr.addQuery('category','hardware');//Hardware is choice label whereas hardware is value
gr.query();
return gr.getRowCount();

 

Also, instead of GlideRecord, GlideAggregate is more efficient to use for COUNT, SUM, MAX etc.