orderby sequence number in list field

r_gissarl_
Mega Expert

Hi,

I need to sort results from a query in the inverse number sequence of a choice list. And I don't know how to do it.

I have a list field in a table (u_catalog). In this field (u_type), I have the following values / sequences :

  • Reseller / 50
  • Customer / 100
  • Master / 200
  • Delegate / 300

When I query the table, I need to get the results in this order :

  1. Delegate
  2. Master
  3. Customer
  4. Reseller

So I was thinking to orderbydesc based on the sequence of the choice list. But I am unsuccessful to access to it... Any idea on how to do this ?

Example of query :

var catalog = new GlideRecord('u_catalog');

      catalog.addQuery('u_active', true);

      catalog.addQuery('u_company', user.company.sys_id);

      gs.log('catalog.getEncodedQuery() ' + catalog.getEncodedQuery(), "getItem");

      catalog.query();

Thanks.

3 REPLIES 3

HV1
Mega Guru

You can use the orderBy and orderByDesc methods in your query.


Ref: GlideRecord - ServiceNow Wiki



Your script will be:


  1. var catalog = new GlideRecord('u_catalog');  
  2.  
  3.       catalog.addQuery('u_active', true);  
  4.       catalog.addQuery('u_company', user.company.sys_id);  
  5.       catalog.orderByDesc(<sequence column name>);
  6.       gs.log('catalog.getEncodedQuery() ' + catalog.getEncodedQuery(), "getItem");  
  7.       catalog.query();



- Hardik Vora


Hello,



This is the point, the sequence number of the choice list is not available to query.



If I order by u_type, I will get the results ordered in this way :


  1. Delegate
  2. Customer
  3. Master
  4. Reseller

Or the opposite with orderbydesc() which is not the result I expect.


not sure, but may need to glide record on sys_choice table.



      var catalog = new GlideRecord('sys_choice');


      catalog.addEncodedQuery('inactive=false^language=en^name=u_catalog^element=u_type');


      catalog.orderByDesc('sequence');


      gs.print('catalog.getEncodedQuery() ' + catalog.getEncodedQuery());


      catalog.query();


    while(catalog.next()){


        gs.print( 'Element:'+catalog.value+' :: Sequence'+catalog.sequence);


        }