Fetch the last 3 recently updated incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
Sometimes we need to quickly identify the most recently updated incidents in ServiceNow—for troubleshooting, reporting, or validation purposes. This can be easily achieved using a Background Script.
Use Case
Retrieve the last 3 incidents based on the most recent update time (sys_updated_on).
System Definition → Scripts – Background:
Solution: Background Script
var gr = new GlideRecord(‘incident’);
gr.orderByDesc(‘sys_updated_on’);
gr.setLimit(3);
gr.query();
while(gr.next()){
gs.print(‘Incident : ’ + gr.number);
}
Script Explanation
GlideRecord('incident')
Initializes a query on the Incident table.orderByDesc('sys_updated_on')
Sorts incidents in descending order based on the last updated date.setLimit(3)
Restricts the result set to only the top 3 records.query()
Executes the database query.while (gr.next())
Iterates through the returned records.gs.print()
Prints the incident numbers to the background script output.
Conclusion
This simple background script is useful for quickly validating recent incident activity without creating reports or lists. You can easily extend it to print additional fields such as short description, state, or assigned to if needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @ZubairQ,
it's good scenario to practice scripting, but if you want to have specific code executed several time, it's better to save it as a fix script...
And for the goal, you don't need to write a single character in your code, you can either create a report or go to list of incident and apply the conditions there... that would display more than 3 incidents, but why would you display 3 incidents?? it seems to be so little number, or what's the actual motivation??
No AI was used in the writing of this post. Pure #GlideFather only
