Write a script to show incident count for each state which are updated today?

HARSHITS3436786
Tera Contributor
 
5 REPLIES 5

Rafael Batistot
Kilo Patron

Hi @HARSHITS3436786 

 

Let me know if this work to you 

 

(function() {
// Get today's date at midnight (00:00:00)
var today = new GlideDateTime();
today.setDisplayValue(gs.nowDate() + ' 00:00:00');

// Query incident table for incidents updated today
var incGR = new GlideAggregate('incident');
incGR.addEncodedQuery('sys_updated_on>=javascript:gs.beginningOfToday()');
incGR.groupBy('state');
incGR.addAggregate('COUNT');
incGR.query();

// Output the result
while (incGR.next()) {
var state = incGR.getDisplayValue('state'); // Get readable state name
var count = incGR.getAggregate('COUNT');
gs.print('State: ' + state + ' | Count: ' + count);
}
})();

 

debendudas
Mega Sage

Hi @HARSHITS3436786 ,

You can use the below script:

var incGR = new GlideAggregate('incident');
incGR.addEncodedQuery('sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()'); // You can add more conditions in this EncodedQuery
incGR.addAggregate('COUNT');
incGR.groupBy('state');
incGR.query();

while (incGR.next()) {
    gs.print(incGR.getDisplayValue('state') + " -> " + incGR.getAggregate('COUNT'));
}

debendudas_0-1754065968777.png

 

If this solution helps you then, mark it as accepted solution ‌‌✔️ and give thumbs up👍

 

Bhanu Bajpai
Tera Expert

Hello @HARSHITS3436786 ,

 

Please find the script with example, kindly refer the SS- 

var gr = new GlideAggregate('incident');
gr.addEncodedQuery("sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()");
gr.groupBy('incident_state');
gr.addAggregate('COUNT');
gr.query();
gs.print("Incident count by state updated today:--");
while (gr.next()) {
    var state = gr.getDisplayValue('incident_state');
    var count = gr.getAggregate('COUNT');
    gs.print(state + ": "+count);

 

BhanuBajpai_0-1754066556020.png

 

 

Thanks,

Bhanu.

 

AndersBGS
Tera Patron
Tera Patron

Hi @HARSHITS3436786 

 

what is the business value to show by each state? An incident updated today can be for changing the state 10 times, changing a category or something third - so what is it from a business perspective you will achieve by doing this?

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/