The CreatorCon Call for Content is officially open! Get started here.

Script

BharatiK
Tera Contributor

Hi,

 

How can we get the incident list which are closed before 24hrs using script?

 

 

Thanks.

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @BharatiK 

Use report and that is low code, dont go for script.

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Dr Atul G- LNG
Tera Patron
Tera Patron

AGLearnNGrow_0-1739289459321.png

 

or 

var gr = new GlideRecord('incident'); // Create a GlideRecord object for the 'incident' table
var currentDate = new GlideDateTime(); // Get the current date and time
currentDate.addHours(-24); // Subtract 24 hours from the current date to get the cutoff point

gr.addQuery('closed_at', '<', currentDate); // Query for incidents where 'closed_at' is before 24 hours ago
gr.addQuery('state', '=', '7'); // Optional: Make sure to filter by 'Closed' state (state=7 is Closed in ServiceNow)
gr.query(); // Execute the query

// Loop through the results and log the incident numbers or handle as needed
while (gr.next()) {
gs.info('Incident Number: ' + gr.number + ', Closed At: ' + gr.closed_at);
}

 

@BharatiK 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @BharatiK 

 

Check this thread
Solved: Re: How to get incident records which are closed i... - ServiceNow Community

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

Thanks & Regards
Viraj Hudlikar.

Nishant8
Giga Sage
Giga Sage

Hello @BharatiK , If you wish to process the Incidents closed before 24 hours, then you can probably make use of Flow designer, below SS can help you write you the same. 

Nishant8_0-1739291846961.png

But, if you are looking for a script specifically then below can be used:

var grInc = new GlideRecord("incident");
var closedIncidents = [];
grInc.addEcodedQuery('closed_atRELATIVELT@minute@ago@24');
grInc.addQuery('closed_at','!=',null);
grInc.query();
while(grInc.next()){
    closedIncidents.push(grInc.getValue('number'));
}
gs.info("incident list is: "+ closedIncidents);
 
Regards,
Nishant