Dynamicall add options to a selectbox

Kamva
Giga Guru

Hi Developers,

 

I have the objective to dynamically add options on the catalog item select box variable when a particular check box has been checked. In an attempt to achieve this objective, I have created a catalog client script (please see below):

Kamva_0-1681672670785.png


function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue == '') {
               return;
        }

     //Type appropriate comment here, and begin script below
      var gr_user = new GlideRecord('sys_user');
          gr_user.addEncodedQuery('active=true^web_service_access_only=false^internal_integration_user=false^roles=timecard_approver');
       gr_user.query();

        while (gr_user.next()){
              g_form.addOption('delegate', gr_user.getValue('user_name'), gr_user.getValue('name'));
        }
}

 

The above catalog client script does not help me achieve my objective instead it produces the error below. Please assist.

Kamva_1-1681672894569.png

 

 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @Kamva ,

May be it's because of addEncodedQuery, try using setEncodedQuery. Try with below script.

 

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

        if (isLoading || newValue == '') {

               return;

        }

 

     //Type appropriate comment here, and begin script below

      var gr_user = new GlideRecord('sys_user');

          gr_user.setEncodedQuery('active=true^web_service_access_only=false^internal_integration_user=false^roles=timecard_approver');

       gr_user.query(respUser);

 

       function respUser(gr_user){

           while (gr_user.next()){

              g_form.addOption('delegate', gr_user.getValue('user_name'), gr_user.getValue('name'));

           }

       }        

}

 

And I don't think even this will return result, why because you are using roles attribute in your encoded query, which is not a valid field in sys_user table.

 

And you are not using the onChange variable value to take a decision in the script, which may cause few other bugs.

 

The best recommendation is to use GlideAjax with client Callable Script Include. Let me know if you need help on this approach.

 

 

 

Thanks,
Anvesh

View solution in original post

2 REPLIES 2

AnveshKumar M
Tera Sage
Tera Sage

Hi @Kamva ,

May be it's because of addEncodedQuery, try using setEncodedQuery. Try with below script.

 

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

        if (isLoading || newValue == '') {

               return;

        }

 

     //Type appropriate comment here, and begin script below

      var gr_user = new GlideRecord('sys_user');

          gr_user.setEncodedQuery('active=true^web_service_access_only=false^internal_integration_user=false^roles=timecard_approver');

       gr_user.query(respUser);

 

       function respUser(gr_user){

           while (gr_user.next()){

              g_form.addOption('delegate', gr_user.getValue('user_name'), gr_user.getValue('name'));

           }

       }        

}

 

And I don't think even this will return result, why because you are using roles attribute in your encoded query, which is not a valid field in sys_user table.

 

And you are not using the onChange variable value to take a decision in the script, which may cause few other bugs.

 

The best recommendation is to use GlideAjax with client Callable Script Include. Let me know if you need help on this approach.

 

 

 

Thanks,
Anvesh

Ramesh Kandhan
Tera Contributor

Hi @Kamva 

 

1.Why you are used catalog client script. You can use reference qualifier to achieve this one and add filter.

2.Regarding this issue please see the console log , It will help to identify exact issue.

 

Thanks,

Ramesh Kandhan