Regarding priority in incident form

SNOW24
Tera Contributor

I want to calculate the count of all the p1,p2 and p3  incidents in last 3 months. please provide script for this.

7 REPLIES 7

abirakundu23
Mega Sage

Hi @SNOW24 ,

Please try below script.

var inc = new GlideAggregate('incident');

inc.addEncodedQuery('priority=1^ORpriority=2^ORpriority=3^sys_created_onONLast 3 months@javascript:gs.beginningOfLast3Months()@javascript:gs.endOfLast3Months()');

inc.addAggregate('COUNT','priority');
inc.orderBy('priority');
inc.query();
var incCount = 0;
while(inc.next())
{
    incCount = inc.getAggregate('COUNT','priority');
gs.info("Priority wise Incident Count :" +incCount);
}
 
Please follow below articles to know more details:
Understanding GlideAggregate (servicenow.com)
Please mark Helpful & correct answer if it's solved your purpose.

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @SNOW24 ,

To calculate the count of incidents with priority P1, P2, and P3 in the last 3 months in ServiceNow, you can use a GlideAggregate query in a script. Here's a sample script you can use:

var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT');
ga.addQuery('priority', 'IN', ['1', '2', '3']);
ga.addQuery('sys_created_on', '>=', gs.beginningOfLast3Months());
ga.addQuery('active', true);
ga.groupBy('priority');
ga.query();

var p1Count = 0;
var p2Count = 0;
var p3Count = 0;

while (ga.next()) {
    var priority = ga.getValue('priority');
    var count = parseInt(ga.getAggregate('COUNT'), 10);
    
    if (priority == '1') {
        p1Count = count;
    } else if (priority == '2') {
        p2Count = count;
    } else if (priority == '3') {
        p3Count = count;
    }
}

gs.info('P1 Count: ' + p1Count);
gs.info('P2 Count: ' + p2Count);
gs.info('P3 Count: ' + p3Count);

 

Please click Accepted as Solution & hit helpful button !!! 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

swathisarang98
Giga Sage
Giga Sage

Hi @SNOW24 ,

 

You can create a simple Dashboard and add single score 3 reports(p1,p2,p3) so that it will be dynamic and you can use it whenever needed

swathisarang98_0-1708679723800.png

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang