How to fetch the last 10 incident records through background script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2023 03:46 AM
How to fetch the last 10 incident records through background script and middle records also. could you please help any one
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2023 03:58 AM - edited 10-01-2023 04:21 AM
Hi @vinod96 ,
Please use below script -
var inc = new GlideRecord('incident');
inc.orderByDesc('sys_created_on');
inc.setLimit(10);
inc.addQuery('number','!=','');
inc.query();
while(inc.next()){
gs.info(inc.number);
}
This will allow you to fetch last 10 incident records
Please mark ✅ Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on the impact.
Regards,
Astik Thombare

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2023 04:02 AM - edited 10-01-2023 04:03 AM
@vinod96 Here is the script for you.
var incidentGR = new GlideRecord('incident');
incidentGR.orderByDesc('sys_created_on');//to get latest incident by created date
incidentGR.setLimit(10);//Set the limit here
incidentGR.query();
while(incidentGR.next());
{
gs.info('Incident Number: '+ incidentGR.getValue('number'));
}
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2023 05:28 AM - edited 10-01-2023 05:29 AM
Hi @vinod96 ,
Please find the below background script for getting the last 10 records:
var incRec = new GlideRecord('incident');
//'orderByDesc' - order the results descending by 'Created' date
incRec.orderByDesc('sys_created_on');
//'setLimit' is used to limit the number of results returned
incRec.setLimit(10);
incRec.query();
while(incRec.next()){
gs.info(" Incident Record: " + incRec.number);
}
Output:
*** Script: Incident Record: INC0000601
*** Script: Incident Record: INC0000055
*** Script: Incident Record: INC0000047
*** Script: Incident Record: INC0000053
*** Script: Incident Record: INC0000052
*** Script: Incident Record: INC0000051
*** Script: Incident Record: INC0000050
*** Script: Incident Record: INC0000049
*** Script: Incident Record: INC0000046
*** Script: Incident Record: INC0000038
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
Mark this as Helpful / Accept the Solution if this helps.