g_form.addOption on Client Script used to show choices on Select Box variable is not honoring the Order of choices specified in the script or on the Variable. How to fix?

bostonsnow
Kilo Guru

Hello,

I have an OnChange Client Script I'm using to show/hide choices on Select Box variable based on the choice selection of another Select Box variable. When choices are shown they are not in the order specified in the script or in the variable. How can I control the display order? 

My script:

-------------------------------------------------------------------------------------

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

if (isLoading || newValue == '') {
return;
}

if(newValue == 'var_1' || newValue == 'var_2') {
g_form.removeOption('var_3', 'choice_1' );

}else{
g_form.addOption('var_3', 'choice_1', 'Choice1' );
g_form.addOption('var_3', 'choice_2', 'Choice2' );

}
}

-------------------------------------------------------------------------------------

I would like Choice1 to be displayed first in the Select Box. However, Choice2 is listed first.

Thanks!

Mike

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi, you must also add an "index" number for placement.

They do not simply order the way you have them lined out here, you must then add a 1 or 2 or 3, etc. for placement.

Like so: 

g_form.addOption('var_3', 'choice_1', 'Choice1', 1);
g_form.addOption('var_3', 'choice_2', 'Choice2', 2);

Reference SN doc for more information: https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideformAddOption_String_String_St...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hi, you must also add an "index" number for placement.

They do not simply order the way you have them lined out here, you must then add a 1 or 2 or 3, etc. for placement.

Like so: 

g_form.addOption('var_3', 'choice_1', 'Choice1', 1);
g_form.addOption('var_3', 'choice_2', 'Choice2', 2);

Reference SN doc for more information: https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideformAddOption_String_String_St...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Perfection, thanks Allen!

Pooja74
Giga Contributor

Hi @Allen A I have same requirement where using GlideAjax in client script to get values .

g_form.addOption('Job_code',rec.value, rec.label);

Kindly help how can we replace the code in such case.

 

Thanks ,

Pooja