How to include NONE in a g.form.addOption from a glide Query.

Mark_Bailey
Mega Guru

I have an on change script where I populate a file based on a p[previous selection. The populated field is mandatory. 

How do I default the field to include none as the default and still make it mandatory to select? 

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

               if(newValue == oldValue){

                              return;

               }

               //remove all items from type

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

               g_form.clearOptions('type');

               //build a new list of dependent options

               var gp = new GlideRecord('u_pshr_account_access_lookup');

               gp.addQuery('u_string_1', newValue);

               gp.addOrderBy('u_string_2');

               gp.query(function (gp){

                              while(gp.next()){

                                             g_form.addOption('type', gp.u_string_2, gp.u_string_2);

                                             }

               });

}

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Mark,

 

Include setValues and Mandatory after while loop and check once.

 

 while(gp.next()){

                                             g_form.addOption('type', gp.u_string_2, gp.u_string_2);

                                             }

g_form.setValue('type', '');

g_form.setMandatory('type',true);

 

Note: Untested

 

-Pradeep Sharma

View solution in original post

2 REPLIES 2

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Mark,

 

Include setValues and Mandatory after while loop and check once.

 

 while(gp.next()){

                                             g_form.addOption('type', gp.u_string_2, gp.u_string_2);

                                             }

g_form.setValue('type', '');

g_form.setMandatory('type',true);

 

Note: Untested

 

-Pradeep Sharma

Mark_Bailey
Mega Guru

Perfect solution, thank you.