how to get the last 10 records in incident table?

SN27
Giga Expert

Hi All,

I wanted to get last 10 records in incident table through background script.

Please help me out here.

Thanks & Regards,

sAtyA.

1 ACCEPTED SOLUTION

Himanshu Gupta1
Mega Guru

HI,

Please find the below script for getting last 10 records.

var incRecord = new GlideRecord('incident');
incRecord.orderByDesc('sys_created_on');
incRecord.setLimit(10);
incRecord.query();
while(incRecord.next()){
gs.info("Incident Record: " + incRecord.number);

}

 

Please mark this helpful if your query is resolved

Best Regards

Himanshu 

View solution in original post

10 REPLIES 10

Daniel Biesiada
Mega Guru


Try below script: 


var inc = new GlideRecord('incident');

inc.setLimit(10);

inc.orderByDesc('sys_created_on');

inc.query();
while(inc.next()){

}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Satya,

if you want last 10 records which got updated then use this

try{

var incRec = new GlideRecord('incident');
incRec.orderByDesc('sys_updated_on');

// if you want last 10 records which are created then use orderByDesc('sys_created_on')

incRec.setLimit(10);
incRec.query();
while(incRec.next()){

// do processing

}

}
catch(ex){
gs.info('Exception is: ' + ex);
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Satya Yadav 

Hope you are doing good.

Did my reply answer your question?

If so, please mark appropriate response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

Thanks!
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

try this code;

display business rule

 

var inc = new GlideRecord('incident');
inc.setLimit(10);
inc.orderByDesc('number');
inc.query();
while(inc.next())
{
gs.addInfoMessage("last 10 records #" + inc.category);
}