Sorting an Advanced Reference Qual

Bradford Shelle
Kilo Guru

Hello all,

I have a reference field that is calling a script include to return an encoded query for a list of records to display via an advanced reference qal. The filter itself works just fine, but now there is a need to always sort the list of records that get returned a predefined way. I tried adding ORDERBY (and ORDERBYDESC) to the end of the encoded query, but I haven't had any luck at all in the list for the reference field being sorted by the field I want it to. Here's an example of one of the encoded query strings where I'm trying to do an ORDERBY.

query = "contract_model=8881e836c3102000b959fd251eba8f29^active=true^starts<=javascript:gs.daysAgoStart(0)^ends>=javascript:gs.daysAgoEnd(0)^u_company=" + inc.company.sys_id.toString() + "^NQvendor_contract=NONE^ORDERBYDESCstarts";

Was wondering if anyone has successfully performed a sort in an encoded query that's used for a Reference Qual before.

Thanks.

2 REPLIES 2

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Unfortunately you cannot pass a sort within an encoded query.   Think of the encoded query as the where clause in a SQL statement and the order by is done outside of that where clause.   Reference qualifier popups leverage whatever user preference sort order that has been previously set.   So if the user had set it to sort on a specific column, then it will remember that and sort on that next time.   You could set a system user preference to sort that table by a certain column, though if users do deviate from that system preference then it will again sort by that preference.



This is the same situation for related list sorting as well.


Hi Ritchie,


I have similar requirement for one field which is reference to country table we need sort it by order and alphabetically also .


Tried the system preference one and wrote one script include and call from the field advance reference qualifier


It is not working can you help me ?


var getCountrysort_list = Class.create();


getCountrysort_list.prototype = {


      initialize: function() {


      },


getCountrysort_list :function() {  


var buildcountrylist = ' ';  


      var countryList = new GlideRecord('core_country');  


countryList.addQuery('active',true);


countryList.orderBy('order');                          


countryList.orderBy('name');    


      countryList.query();  


  while(countryList.next()) {  


  if (buildcountrylist.length > 0) {      


        buildcountrylist += (',' + countryList.name);  


  }  


  }  


  var resultset = 'sys_idIN' + buildcountrylist;  


 


      return resultset;  


} ,




      type: 'getCountrysort_list'


};