- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 08:45 PM
Hi Everyone,
If run the below script
var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.orderByDesc('sys_created_at');
inc.setLimit(10);
inc.query();
while(inc.next()){
gs.print(inc.number);
}
Getting below out put
[0:00:00.031] Script completed in scope global: script
Script execution history and recovery available here
*** Script: INC0008001
*** Script: INC0010078
*** Script: INC0010074
*** Script: INC0000015
*** Script: INC0000016
*** Script: INC0000017
*** Script: INC0000018
*** Script: INC0000019
*** Script: INC0000020
*** Script: INC0000025
but how would i these latest or not
Please clarify
Please share me the correct script to print latest 10 incidents
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 08:52 PM
Small correction. it should be sys_created_on and not sys_created_at
you are using orderByDesc() so that guarantees latest records are picked based on sys_created_on
you can verify this on incident list by sort z to a on the Created Date field
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 09:32 PM
@NaveenKumarSN Please update your script as follows.
var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.orderByDesc('sys_created_on');
inc.setLimit(10);
inc.query();
while(inc.next()){
gs.print(inc.number);
}
To verify if the output is correct or not, you need to go to incident list and sort it by created on in descending order, match the first 10 incident numbers are matching with you numbers printed in the script. If it matches than the script is working fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 09:38 PM
Hi @NaveenKumarSN
Order the results by the sys_created_on or sys_updated_on field in descending order, and then limit the result set to 10.
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on'); // Order by creation date in descending order (latest first)
gr.setLimit(10);
gr.query();
var count = 0;
while (gr.next()) {
count++;
gs.print("Incident #" + count + ":");
gs.print("Number: " + gr.number);
gs.print("Created On: " + gr.sys_created_on.getDisplayValue());
gs.print(""); // Print a blank line for better readability
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 10:53 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 08:52 PM
Small correction. it should be sys_created_on and not sys_created_at
you are using orderByDesc() so that guarantees latest records are picked based on sys_created_on
you can verify this on incident list by sort z to a on the Created Date field
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 09:32 PM
@NaveenKumarSN Please update your script as follows.
var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.orderByDesc('sys_created_on');
inc.setLimit(10);
inc.query();
while(inc.next()){
gs.print(inc.number);
}
To verify if the output is correct or not, you need to go to incident list and sort it by created on in descending order, match the first 10 incident numbers are matching with you numbers printed in the script. If it matches than the script is working fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 10:53 PM
Getting undefined for 10 times
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2024 09:38 PM
Hi @NaveenKumarSN
Order the results by the sys_created_on or sys_updated_on field in descending order, and then limit the result set to 10.
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on'); // Order by creation date in descending order (latest first)
gr.setLimit(10);
gr.query();
var count = 0;
while (gr.next()) {
count++;
gs.print("Incident #" + count + ":");
gs.print("Number: " + gr.number);
gs.print("Created On: " + gr.sys_created_on.getDisplayValue());
gs.print(""); // Print a blank line for better readability
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma