How to Query the Latest Record from a Table in ServiceNow
- nathanlee142
- Mar 20
- 3 min read
Updated: Mar 29

Retrieving the most recent record from a ServiceNow table is a common requirement for system administrators and developers. Whether you're creating business rules, background scripts, or scheduled jobs, accurately querying the latest entry ensures your automation processes remain reliable and efficient. In this article, you'll learn the correct way to query the latest record from any ServiceNow table, along with clear explanations and practical examples to implement this approach effectively.
How to Correctly Retrieve the Latest Record
Understanding the Problem
Suppose you want to identify the most recently created record within the Incident table. Simply querying records without specifying a sorting method will not necessarily return the newest record. Therefore, you need to clearly instruct ServiceNow how to sort and limit records to get the exact entry you need.
Step-by-Step Solution
Follow these steps to reliably fetch the latest record from any table using a GlideRecord script:
Step 1: Initialize a GlideRecord object
Begin by specifying the table you are querying:
var gr = new GlideRecord('incident');
Step 2: Order the records by creation date in descending order
The orderByDesc() method ensures that the newest record appears first.
Ensures records are sorted in descending order based on their creation timestamp:
gr.orderByDesc('sys_created_on');
Step 3: Limit your query to a single result
To ensure only the latest record is fetched, set the query limit to one:
gr.setLimit(1);
Step 4: Execute the query and retrieve the record
Execute the query and access the retrieved data:
gr.query();
if (gr.next()) {
gs.info(gr.number + ' created on ' + gr.sys_created_on);
}
Practical Example
Here is a complete example combining all the steps above:
// Querying the most recent incident record
var incidentGR = new GlideRecord('incident');
incidentGR.orderByDesc('sys_created_on');
incidentGR.setLimit(1);
incidentGR.query();
if (incidentGR.next()) {
gs.info("Latest Incident: " + incidentGR.number + ", created on: " + incidentGR.sys_created_on);
} else {
gs.info("No records found.");
}
When you run this script, it will reliably fetch and print the number and creation date of the newest incident in the system.
Alternative Use Cases and Additional Tips
Retrieving Multiple Recent Records
To fetch more than one recent record, simply change the limit parameter:
// Example to retrieve the latest 10 incidents
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(10);
gr.query();
while (gr.next()) {
gs.info(gr.number + ' - ' + gr.sys_created_on);
}
This approach can be very useful for generating quick reports or summaries via background scripts.
Using Other Fields for Sorting
You aren't restricted to using sys_created_on. You can also use fields like sys_updated_on to find recently updated records instead of recently created ones.
gr.orderByDesc('sys_updated_on');
Common Mistakes to Avoid
Not specifying sorting: Failing to use orderByDesc() will not guarantee the latest record appears first.
Incorrect field names: Ensure the correct field names (sys_created_on, sys_updated_on) are used, or your script won't function as intended.
Missing setLimit(): Without limiting results, the script may retrieve a large number of records unnecessarily, potentially impacting performance and increasing execution time.
Conclusion
Querying the most recent record from a ServiceNow table is straightforward when using the GlideRecord method correctly. Remember the key points:
Always use orderByDesc() to sort by the most recent entry.
Use setLimit(1) to efficiently return just the latest record.
Customize your query further based on your specific use case.
Next Steps
Review existing scripts or automations to ensure they use the recommended approach.
Explore additional sorting fields (e.g., sys_updated_on) to suit your specific needs.
Experiment with background scripts and scheduled jobs to automate regular tasks involving the latest records.
By following these guidelines, you'll ensure accurate, efficient, and reliable data retrieval in your ServiceNow environment.