- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 03:17 AM
I know that it is possible to sort on multiple fields by adding multiple orderBy clauses to a query but I need to sort one field ascending and one descending. If I add one orderByclause and one orderByDesc clause the second one is ignored.
Is there any way I can sort one field ascending and one descending?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 03:29 AM
Hi,
Yes, you can add orderBy and ordebydesc clause in gliderecord query.
Refer below, it is working as to sort incidents based on state and desc order for priority:
var gr = new GlideRecord('incident');
gr.orderBy('state'); // Ascending Order
gr.orderByDesc('priority'); // Ascending Order
//gr.addEncodedQuery(queryString);
gr.query();
while (gr.next()) {
gs.info(gr.number+" "+gr.state +" P:"+gr.priority);
}
----Result of above script------
INC Number State(ASC) Priority(DESC)
*** Script: INC0010001 1 P:5
*** Script: INC0010112 1 P:5
*** Script: INC0010111 1 P:5
*** Script: INC0000057 1 P:5
*** Script: INC0000039 1 P:5
*** Script: INC0000058 1 P:5
*** Script: INC0009009 1 P:4
*** Script: INC0007002 1 P:4
*** Script: INC0000046 1 P:3
*** Script: INC0009001 1 P:3
*** Script: INC0000059 1 P:3
*** Script: INC0007001 1 P:1
*** Script: INC0009005 1 P:1
*** Script: INC0000029 2 P:5
*** Script: INC0000020 2 P:5
*** Script: INC0000047 2 P:3
*** Script: INC0000037 2 P:3
*** Script: INC0000041 2 P:3
*** Script: INC0000019 2 P:2
*** Script: INC0000027 2 P:2
*** Script: INC0000049 2 P:2
*** Script: INC0000044 2 P:2
*** Script: INC0000051 2 P:1
*** Script: INC0000055 2 P:1
*** Script: INC0000003 2 P:1
*** Script: INC0000050 2 P:1
*** Script: INC0000052 2 P:1
*** Script: INC0000031 2 P:1
*** Script: INC0000053 2 P:1
*** Script: INC0000018 2 P:1
*** Script: INC0000015 2 P:1
*** Script: INC0000025 2 P:1
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 03:29 AM
Hi,
Yes, you can add orderBy and ordebydesc clause in gliderecord query.
Refer below, it is working as to sort incidents based on state and desc order for priority:
var gr = new GlideRecord('incident');
gr.orderBy('state'); // Ascending Order
gr.orderByDesc('priority'); // Ascending Order
//gr.addEncodedQuery(queryString);
gr.query();
while (gr.next()) {
gs.info(gr.number+" "+gr.state +" P:"+gr.priority);
}
----Result of above script------
INC Number State(ASC) Priority(DESC)
*** Script: INC0010001 1 P:5
*** Script: INC0010112 1 P:5
*** Script: INC0010111 1 P:5
*** Script: INC0000057 1 P:5
*** Script: INC0000039 1 P:5
*** Script: INC0000058 1 P:5
*** Script: INC0009009 1 P:4
*** Script: INC0007002 1 P:4
*** Script: INC0000046 1 P:3
*** Script: INC0009001 1 P:3
*** Script: INC0000059 1 P:3
*** Script: INC0007001 1 P:1
*** Script: INC0009005 1 P:1
*** Script: INC0000029 2 P:5
*** Script: INC0000020 2 P:5
*** Script: INC0000047 2 P:3
*** Script: INC0000037 2 P:3
*** Script: INC0000041 2 P:3
*** Script: INC0000019 2 P:2
*** Script: INC0000027 2 P:2
*** Script: INC0000049 2 P:2
*** Script: INC0000044 2 P:2
*** Script: INC0000051 2 P:1
*** Script: INC0000055 2 P:1
*** Script: INC0000003 2 P:1
*** Script: INC0000050 2 P:1
*** Script: INC0000052 2 P:1
*** Script: INC0000031 2 P:1
*** Script: INC0000053 2 P:1
*** Script: INC0000018 2 P:1
*** Script: INC0000015 2 P:1
*** Script: INC0000025 2 P:1
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 04:26 AM
Thanks Asha. I have realised that the problem was due to the data in one of the fields I was using but it's good to know that this method does work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2021 06:09 PM
Perfect, this worked. Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 03:33 AM
Hi,
It should be possible with the below code.
//source field is in ascending and time_of_event is in descending.
var x = new GlideRecord('em_event');
var query = 'ORDERBYsource^ORDERBYDESCtime_of_event';
x.addEncodedQuery(query);
x.query();
while(x.next()){
gs.info(x.sys_id);
}
This is tested and working fine.