Does orderByDesc work in a GlideAggregate script?

Victor Ruiz
Tera Guru

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()) {

 

 

4 REPLIES 4

Syvo
Mega Guru

Hi,

Can you describe the requirement, what are you trying to achieve here?

Did you try with agg.setGroup(false)?

Nikhil Phalke
Kilo Guru

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

Basically I am trying to retrieve the latest 3 records in the task_ci Table but there arent many fields to sort by.

 

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

We always use something like this with Aggregate. Yes orderByDesc creates issue with aggregate API.
find_real_file.png

 

Thanks,
Ashutosh