sorting the column through Script

Chandu12
Mega Expert

find_real_file.png

this is already created string field for the column, since the string is given sorting is not happening

find_real_file.png

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

find_real_file.png

find_real_file.png

find_real_file.png

tried this code actually ...was not sorting

find_real_file.png

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

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

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.


Chuck Tomasi
Tera Patron

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.


johnolivermendo
ServiceNow Employee
ServiceNow Employee

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.