Pring Lat 5 records of current logged in user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2024 02:34 AM
I am sure the below script will print the last 5 records created on the system but am looking to print last 5 records assigned to the logged in user , could anyone please help me with the change on the below script.
var gr = new GlideRecord('incident);
gr.orderByDesc('sys_created_on');
gr.SetLimit(5);
gr.query();
while(gr.next(){
gs.print('incident number: '+gr.number);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:43 AM
@ Anurag Tripathi Could you please give some idea on to this
I tried below script but still not working as expected

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:54 AM
Hi there,
You would need at least to fix the below, as that will never work:
gr.SetLimit(5);
Its setLimit, not SetLimit.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:58 AM
Please use below code and test it using background scripts. Probably, setLimit() would be the culprit as suggested by @Mark Roethof .
var currentUser = gs.getUserID();
var gr = new GlideRecord('incident');
gr.addQuery('caller_id='+currentUser);
gr.orderByDesc('sys_created_on');
gr.setLimit(5);
gr.query();
while(gr.next()){
gs.print('incident number: '+gr.number);
}
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 01:02 AM
Hi @Chiranjeevi K ,
Please check with below script.
Abhishek Dalvi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 01:13 AM
Sure, you can modify the script to filter the incidents assigned to the current logged in user. Here is the updated script: javascript var gr = new GlideRecord('incident'); gr.addQuery('assigned_to', gs.getUserID()); // filter incidents assigned to the current user gr.orderByDesc('sys_created_on'); gr.setLimit(5); gr.query(); while(gr.next()) { gs.print('incident number: ' + gr.number); } Here are the changes summarized: - Added a new line gr.addQuery('assigned_to', gs.getUserID()); to filter incidents assigned to the current user. - gs.getUserID() is used to get the sys_id of the current logged in user. - This line should be added before gr.orderByDesc('sys_created_on'); to ensure the filter is applied before sorting and limiting the results.