- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2015 01:03 PM
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:
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2015 01:14 PM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2015 01:14 PM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2015 01:21 PM
That's great Travis, thank you. Do you know how I might add the '-None-' option at the top?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2015 01:32 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2015 01:46 PM
Brilliant. You guys amaze me. Thanks Travis.