Regarding groupby priority

SNOW24
Tera Contributor

how to write script logic in group by priority.

I have tried below one but its not working. please check this once and let me know correct logic.

var gr= new GlideAggregate('incident');
gr.addAggregate('GROUPBY', 'priority');
gr.orderByAggregate('priority');
gr.query();
while (gr.next()) {
var priority = gr.getAggregate('priority');
if (priority === '1 - Critical') {

} else if (priority === '2 - High') {

} else if (priority === '3 - Moderate') {

} else if (priority === '4 - Low') {

}
}

3 REPLIES 3

Sagar Pagar
Tera Patron

Hi @SNOW24,

 

Try this modified scripts -

var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT', 'priority');
agg.orderBy('priority');
agg.groupBy('priority');
agg.query();
while (agg.next()) {
	//do things on the results
	var incidentCount = agg.getAggregate('COUNT', 'priority');
	var priority = agg.getDisplayValue('priority');

	var state = agg.getDisplayValue('state');
	var assigned_to = agg.getDisplayValue('assigned_to');


	if (priority === '1 - Critical') {
		gs.info("P1");
		gs.info('Display the count {0} and priority {1}  assigned to {2} and state {3}', [incidentCount, priority, assigned_to, state]);
	} else if (priority === '2 - High') {
		gs.info("P2");
		gs.info('Display the count {0} and priority {1}  assigned to {2} and state {3}', [incidentCount, priority, assigned_to, state]);
	} else if (priority === '3 - Moderate') {
		gs.info("P3");
		gs.info('Display the count {0} and priority {1}  assigned to {2} and state {3}', [incidentCount, priority, assigned_to, state]);
	} else if (priority === '4 - Low') {
		gs.info("P4");
		gs.info('Display the count {0} and priority {1}  assigned to {2} and state {3}', [incidentCount, priority, assigned_to, state]);
	}

}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Dhananjay Pawar
Kilo Sage

Hi,

By this simple way you will get the incident count per priority.

 

var gr= new GlideAggregate('incident');
gr.addAggregate('count', 'priority');
gr.query();
while (gr.next()) {
var priority = gr.getAggregate('count', 'priority');
var prioVal = gr.getValue('priority');
gs.print("For priority " + prioVal +" we have total "+priority+ " incidents.");
}
 
If you have any additional requirements, then please do share so accordingly people will help you.
 
Thanks.

Hi @SNOW24 ,

If this is resolved then accept the solution so in future other people will refer same.

 

Thanks.