Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

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 - LNG
Tera Patron

Hi @BharatiK 

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

 

Dr Atul - LNG
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 

Viraj Hudlikar
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
Tera 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