Choice list - Alphabetical order

n_kishore
Giga Contributor

Hi ,

It's very annoying to keep sorting the choice list every time when i add new values or i have change the order/sequence of the value every time.
Is there any way where i can sort alphabetical order wise automatically or by click of a button.

Please educate me on this

Thanks,
Raghu loganathan.

4 REPLIES 4

Kalaiarasan Pus
Giga Sage

Good question. I do not know of any OOB option to do this.



Hence, I have created a button to do this.



1. Create a UI action 'Sort Choices' on sys_choice table and select 'List Choice' checkbox.



2. Select the 'client' checkbox and add the following in 'Onclick' field.


sortChoices();



3. Add the below condition to the button


current.name!='' && current.element!='' && !current.inactive



4. Paste the below script


function sortChoices() {


  var ajaxHelper = new GlideAjax('ChoiceListHelper');


  ajaxHelper.addParam('sysparm_name', 'sortChoiceList');


  ajaxHelper.addParam('sysparm_choiceValue', rowSysId);


  ajaxHelper.getXML(function (response)


  {


  if(response.responseXML.documentElement.getAttribute("answer")=='Sorting completed')


  {


  location.reload();


  }


  }


  );


}



5. Create a script include called 'ChoiceListHelper' and select the 'Client callable' checkbox.


6. Paste the below script



var ChoiceListHelper = Class.create();


ChoiceListHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  sortChoiceList : function() {



  var returnAnswer='';


  var choiceValue = this.getParameter('sysparm_choiceValue');


  var sequenceCounter=0;


  var getChoice =   new GlideRecord('sys_choice');


  if(getChoice.get(choiceValue))


  {


  var getAllChoices = new GlideRecord('sys_choice');


  getAllChoices.addEncodedQuery('name='+getChoice.name+'^element='+getChoice.element+'^inactive=false^language='+getChoice.language);


  getAllChoices.orderBy('label');


  getAllChoices.query();


  while(getAllChoices.next())


  {


  getAllChoices.sequence=sequenceCounter;


  sequenceCounter++;


  getAllChoices.update();


  }


  }


  return 'Sorting completed';



  }


});



You can play around the script to tailor your specific needs.


acirovic1
Giga Expert

If you have a choice list long enough you need to sort it alphabetically, it's better to use a reference to a lookup table. The users will then be able to sort the list themselves and use autocomplete functionality.


PeterWiles
Kilo Sage

On the choice list table, there is an order field. If you remove the numbers for your specific list, it will sort the list alphabetically. If you then amend the order in anyway via the UI, it will reset the order value and you will need to do it manually again.



Once you have done the above, when you add new values, just add them to the bottom and then save. Don't order them. SN won't enforce the order field and it will carry on alphabetically.



Not tested in Geneva but works pre-Geneva.



Pete


Lexter
Mega Contributor

Set all items in the sequence field to 0, then items will be set to alphabetical order.