orderby sequence number in list field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2016 08:53 AM
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 :
- Delegate
- Master
- Customer
- 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.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2016 10:22 AM
You can use the orderBy and orderByDesc methods in your query.
Ref: GlideRecord - ServiceNow Wiki
Your script will be:
- var catalog = new GlideRecord('u_catalog');
- catalog.addQuery('u_active', true);
- catalog.addQuery('u_company', user.company.sys_id);
- catalog.orderByDesc(<sequence column name>);
- gs.log('catalog.getEncodedQuery() ' + catalog.getEncodedQuery(), "getItem");
- catalog.query();
- Hardik Vora
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2016 12:54 AM
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 :
- Delegate
- Customer
- Master
- Reseller
Or the opposite with orderbydesc() which is not the result I expect.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2016 01:23 AM
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);
}