Does orderByDesc work in a GlideAggregate script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2020 08:32 AM
Does orderByDesc work in a GlideAggregate script? The below code will not run when I add the section in red, once removed it works fine:
var test = 'task.sys_class_name=u_custom_table_name';
var theList = [];
var agg = new GlideAggregate('task_ci');
agg.addEncodedQuery(test);
agg.orderByDesc('sys_created_on');
agg.setLimit(3);
agg.addAggregate('COUNT', 'ci_item');
agg.addHaving('COUNT', '>', 2);
agg.query();
while (agg.next()) {
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2020 08:42 AM
Hi,
Can you describe the requirement, what are you trying to achieve here?
Did you try with agg.setGroup(false)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2020 08:42 AM
Hi Victor,
ServiceNow system does not allow you to sort using date/time field. You are trying orderByDesc("sys_created_on") which is a date time field. Try to use another field to sort the record.
Bassically in time (12hr format) there can be 1 am as well as 1 pm, so the system cannot differentiate between them. That is why the system does not allow you to sort using date/time field.
There are other fields type as well which system does not sort like image, script, wiki, document etc types of fields.
Please mark Helpful and Correct based on Impact.
Regards
Nikhil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2020 09:30 AM
Basically I am trying to retrieve the latest 3 records in the task_ci Table but there arent many fields to sort by.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2020 08:52 AM