Ordering options in a Select Box Variable

Wayne Richmond
Tera Guru

I've used this client script to populate a Select Box Variable because I need dependent values. However, the select box seems to list the options in a random order. How do I get the order to follow the sequence of the field it references? They are ordered by 'sequence' on the Incident table.

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

if(newValue == oldValue){

return;

}

//remove all items from function drop down to start

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

g_form.clearOptions('u_helios_function');

//build a new list of dependent options

var gp = new GlideRecord('sys_choice');

gp.addQuery('dependent_value', newValue);

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

gp.query();

while(gp.next()){

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

}

}

The random order of the list:

helios_functions.png

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

Hi Wayne,



Your script is ordering the options according to the order in which the gp query returns them.   Add an Order By statement to your query using the orderBy function on GlideRecord.   Example:



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


if(newValue == oldValue){


return;


}


//remove all items from function drop down to start


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


g_form.clearOptions('u_helios_function');



//build a new list of dependent options


var gp = new GlideRecord('sys_choice');


gp.addQuery('dependent_value', newValue);


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


gp.orderBy('sequence'); //Added orderBy


gp.query();


while(gp.next()){


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


}


}


View solution in original post

4 REPLIES 4

tltoulson
Kilo Sage

Hi Wayne,



Your script is ordering the options according to the order in which the gp query returns them.   Add an Order By statement to your query using the orderBy function on GlideRecord.   Example:



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


if(newValue == oldValue){


return;


}


//remove all items from function drop down to start


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


g_form.clearOptions('u_helios_function');



//build a new list of dependent options


var gp = new GlideRecord('sys_choice');


gp.addQuery('dependent_value', newValue);


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


gp.orderBy('sequence'); //Added orderBy


gp.query();


while(gp.next()){


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


}


}


That's great Travis, thank you. Do you know how I might add the '-None-' option at the top?


Hi Wayne,



Absolutely.   First, on the Variable record, make sure you mark the "Include None" field.   I am not sure if this is specifically required, but the variable may not recognize "--None--" as an option if you don't.   Then, include a line like the following to add it in the script:



g_form.addOption('u_helios_function', '', '--None--');



Include that line before you start looping through the query results and it will be the first added at the top of the list.   The --None-- option is represented as an empty string value on all the other choice lists.


Brilliant. You guys amaze me. Thanks Travis.