is there a way to sort the options that we add to a list with g_form.addOption()?

sharmavikrant
Kilo Expert

is there a way to sort the options that we add to a list with g_form.addOption()?

16 REPLIES 16

Hi Chandana,



Thanks for that sorry for not being clear, I mean was I was not able to get my head around where to use the array, Below is the script if you don't mind helping me to sort that out?



function onChange(control, oldValue, newValue, isLoading) {


  if(newValue == oldValue){


  return;


  }


  //remove all items from subcat drop down to start


  // Used the g_form.clearOptions() function instead of g_form.removeOption() function


  g_form.clearOptions('service_name');



  //build a new list of dependent options


  var gp = new GlideRecord('sys_choice');


  gp.addQuery('dependent_value', newValue);


  gp.addQuery('element', 'service_name');


  gp.query();


  while(gp.next()){


  g_form.addOption('service_name', gp.value, gp.label);


  }


}


Hi Vikrant,



Can you tell me on what basis you would want to sort ? is it based on value or label and can you give me couple of sample values and labels.


sharmavikrant
Kilo Expert

It would be lable,


Below are some sample lables:


Dynamo
FAST
GET

Hi Vikrant,



I had sent you a message, couldnt type the whole code again.


Let me know if you have received it, otherwise I will try to put it here again.


sharmavikrant
Kilo Expert

Hi Chandana, Got it. Thanks for the help.