- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Hello Community,
Hope you are doing well!
After a long time, I’m excited to share some simple and practical scripting tips in ServiceNow. I hope this will be helpful for you in your day-to-day development work.
1. SetLimit()
setLimit() It is a GlideRecord method which is typically used to get thousands of records from a database. In a simple way, we only get an exact number of records by adding this method.
2. OrderBy() & OrderByDesc()
orderBy()→ Returns records in ascending order (A → Z)orderByDesc()→ Returns records in descending order (Z → A)
🔹Example Scenario
Let’s retrieve the first 5 incident records:
Created 1 year ago
State not in Closed, Resolved, or Cancelled
✅Ascending Order Example
var incidentRec = new GlideRecord('incident');
incidentRec.addEncodedQuery('sys_created_onONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()^stateNOT IN6,7,8');
incidentRec.setLimit(5);
incidentRec.orderBy('number');
incidentRec.query();
while(incidentRec.next()){
gs.print("CV Count = " + incidentRec.number);
}👉This will return incident numbers in ascending order.
🔹Converting Records to Array
var incArray = [];
var incidentRec = new GlideRecord('incident');
incidentRec.addEncodedQuery('sys_created_onONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()^stateNOT IN6,7,8');
incidentRec.setLimit(5);
incidentRec.orderBy('number');
incidentRec.query();
while(incidentRec.next()){
gs.print("CV Count = " + incidentRec.number);
incArray.push(incidentRec.getValue('number'));
}
var arrayRecords = incArray.join(',');
gs.print("Records = " + arrayRecords);👉This will return all incident numbers as a comma-separated string.
🔹Descending Order Example
var incidentRec = new GlideRecord('incident');
incidentRec.addEncodedQuery('sys_created_onONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()^stateNOT IN6,7,8');
incidentRec.setLimit(5);
incidentRec.orderByDesc('number');
incidentRec.query();
while(incidentRec.next()){
gs.print("CV Count = " + incidentRec.number);
}👉This will return incident numbers in descending order.
🔹Descending Order with Array
var incArray = [];
var incidentRec = new GlideRecord('incident');
incidentRec.addEncodedQuery('sys_created_onONOne year ago@javascript:gs.beginningOfOneYearAgo()@javascript:gs.endOfOneYearAgo()^stateNOT IN6,7,8');
incidentRec.setLimit(5);
incidentRec.orderByDesc('number');
incidentRec.query();
while(incidentRec.next()){
gs.print("CV Count = " + incidentRec.number);
incArray.push(incidentRec.getValue('number'));
}
var arrayRecords = incArray.join(',');
gs.print("Records = " + arrayRecords);👉This will return incident numbers in descending order as a comma-separated string.
That's all from my side.
If you all like it or it is helpful for you, Please mark it as helpful or bookmark this article for future refence.
Regards,
Chaitali.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
