how to get most recent created incident

EswarS
Tera Contributor

Hi Team,

Can anyone help me  on this

how to get the latest created incident or sctask through scripts

Thanks in advance

3 ACCEPTED SOLUTIONS

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @EswarS 

 

What is the use case you have here? You can do it via list view and sort by created date.

 

Try this script:

 

var incidentList = [];
var incident1 = new GlideRecord('incident');
incident1.query();
while(incident1.next()){
    var incident2 = new GlideRecord('incident');
 
    incident2.orderByDesc('sys_created_on');
    incident2.query();
    if(incident2.next()){
        incidentList.push(incident2.number.toString());
    }
}
var uniqueIncidentList= new ArrayUtil().unique(incidentList);
gs.print(uniqueIncidentList);
 
I tested this in my PDI and it is working fine.
*************************************************************************************************************
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]

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

View solution in original post

OlaN
Giga Sage
Giga Sage

Hi,

Not sure what you want to do with the record you retrieve through the script, but here's something to show how to do.

 

var latestIncGR = new GlideRecord('incident');
latestIncGR.orderByDesc('sys_created_on');  // sort by created
latestIncGR.setLimit(1);  //only retrieve one record
latestIncGR.query();
if (latestIncGR.next()){
	gs.info('Number: ' + latestIncGR.getValue('number'));
}

View solution in original post

Mimi Edet
Tera Guru

Hi @EswarS

 

You can execute this script in background script or any other script execution environment in ServiceNow. I recently created three incidents on my PDI, and use this script in background script to test. Kindly find attached.

 

Regards,

Mimi Edet

LinkedIn: https://www.linkedin.com/in/edet-promise/

=========================================================
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

 

 

View solution in original post

5 REPLIES 5

EswarS
Tera Contributor

Hi Mimi,
Thank U for the Support 
Ur script worked Fine.