sorting the column through Script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2017 08:43 AM
this is already created string field for the column, since the string is given sorting is not happening
what would be the best solution for this, where sorting should happen. Please help me in solving this.
Tried this
---------------
Tried Before query, before insert, Update
tried this code actually ...was not sorting
Note : tried this also but was not useful please correct me if i am wrong
orderBy/orderByDesc
You can order the results of your recordset by using 'orderBy' and/or 'orderByDesc' as shown below.
//Find all active incidents and order the results ascending by category then descending by created date
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.orderBy('category');
gr.orderByDesc('sys_created_on');
gr.query();
https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/
Regards,
Chalan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2017 08:55 AM
It's probably not sorting how you want it to because it's a string field rather than an integer field. Depending on whether this is already being used in your production instance, you may want to create a new field with the type integer and use that field instead. If you haven't deployed this to prod yet, you can delete the field and use the new one, but doing that will cause you to LOSE ANY DATA IN THAT COLUMN, so I would only do that if you haven't moved it to prod yet. If you're already using this in prod you could do something like create a new field and copy the data over without deleting the old one.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2017 08:57 AM
You may want to consider changing it from a string to an integer so it sorts properly. Strings will always sort alphabetically. Meaning you'll get lists like this with strings:
1,14,2,21,4,7
instead of
1, 2, 4, , 7, 14, 21
with integers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2017 09:51 AM
Hey Chalan,
Did my answer from your previous thread Remove Decimal(.0) from ( Days Open) Field eg:209.0 not work for you?
The solution from that thread should have properly made your field an Integer type and would resolve your issues here. Let me know if you need more clarification or if there is something blocking you from accomplishing this.