Set default choice through catalog client script

Kim Jenson
Kilo Contributor

Hello experts,

I'm new to coding, and trying to accomplish a task, not sure how this can be done.

I have this If/else statement that i'm checking to see set choice list for the variable, which works as intended. However, I want to set choices depending on which is true. If 'answer = 2 then default type to 'Add User' else default 'type' to 'Request info'. Can this be possible within this If/else condition?

  if (answer == 2) {

  g_form.addOption('type', 'Add User', 'Add User');

  g_form.addOption('type', 'Edit User', 'Edit User');

  } else {

  g_form.addOption('type', 'Request Info', 'Request Info');

  g_form.addOption('type', 'Request Access', 'Request Access');

  }

1 ACCEPTED SOLUTION

Thanks for the details Kim. Here is the updated code.



  if (answer == 2) {


  g_form.addOption('type', 'Add User', 'Add User');


  g_form.addOption('type', 'Edit User', 'Edit User');


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


  } else {


  g_form.addOption('type', 'Request Info', 'Request Info');


  g_form.addOption('type', 'Request Access', 'Request Access');


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


  }


View solution in original post

7 REPLIES 7

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Kim,



Here is the updated code.



  if (answer == 2) {


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



  } else {


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



  }



Please let me know if you have any questions.


Hi Pradeep,



Thanks for your reply. But this is not what I'm trying to do. I have to display the 2 choices if answer = 2 regardless. The idea is to not show all 4 choices within the choice list. But instead of showing '--None--', I want it to default to the 'Add User' ELSE 'Request Info'.


Thanks for the details Kim. Here is the updated code.



  if (answer == 2) {


  g_form.addOption('type', 'Add User', 'Add User');


  g_form.addOption('type', 'Edit User', 'Edit User');


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


  } else {


  g_form.addOption('type', 'Request Info', 'Request Info');


  g_form.addOption('type', 'Request Access', 'Request Access');


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


  }


Hi Pradeep,



That works as intended. But now I'm seeing another problem. My UI Policy I have on that 'Add User' to show other variables when 'type' = 'Add User' does not execute. My guess is because of 2 rules running at the same time, and this onChange client script takes precedence?