Hide options in a variable

John Vo1
Tera Guru

Is there   a script that can be place to remove options under a variable.   UI Policy will just hide the variable and that's not what I want.

For example:

If Remove User Access is selected for Type:

find_real_file.png

I want only N/A to show and not the other 2 options for Ready Only or Write.

find_real_file.png

1 ACCEPTED SOLUTION

Here you go



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


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


  return;


  }



  if (newValue== 'Remove User Access') {


  g_form.removeOption('edit_or_read_only', 'Read Only');


  g_form.removeOption('edit_or_read_only', 'Write');


  }


  else{


  if(g_form.getOption('edit_or_read_only','Read Only')==null){


  g_form.addOption('edit_or_read_only','Read Only','Read Only');


  }


  if(g_form.getOption('edit_or_read_only','Write')==null){


  g_form.addOption('edit_or_read_only','Write','Write');


  }


  }



}


View solution in original post

25 REPLIES 25

HI Wesley,



I haven't tried the g_form.removeOption() when using a reference field in "choice mode". Not sure if that will work. It's an interesting experiment. Adding the options back in... even better. I would start by using the same format of g_form.addOption() with the display value, the sys_id (as the value), and order. Again, not sure if any of it is supported in this scenario, but it can't hurt to do a quick little test.



Let me know what you find.



Reference:GlideForm (g form) - ServiceNow Wiki


Client Scripts - ServiceNow Wiki


Hi johnvo



After removing the options you need to add them back if you select a different type. Here is your complete onChange client script on "Type" variable



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


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


  return;


  }



  if (newValue== 'Choice value of Remove User Access') {


  g_form.removeOption('variable name of read only or write', 'choice value of Read only');


  g_form.removeOption('variable name of read only or write', 'choice valeu of Write');


  }


  else{


  if(g_form.getOption('variable name of read only or write','choice value of Read only')==null){


  g_form.addOption('variable name of read only or write','choice value of read only','choice label of read only');


  }


  if(g_form.getOption('variable name of read only or write','choice value of write')==null){


  g_form.addOption('variable name of read only or write','choice value of write','choice label of write');


  }


  }



}


The read only and write options stills shows up when Remove access only is selected.   Here is what's in the onchange script.



find_real_file.png


Do not copy the code as is. You need to replace the variable names choice values and choice labels as highlighted below



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


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


  return;


  }



  if (newValue== 'Choice value of Remove User Access') {


  g_form.removeOption('variable name of read only or write', 'choice value of Read only');


  g_form.removeOption('variable name of read only or write', 'choice valeu of Write');


  }


  else{


  if(g_form.getOption('variable name of read only or write','choice value of Read only')==null){


  g_form.addOption('variable name of read only or write','choice value of read only','choice label of read only');


  }


  if(g_form.getOption('variable name of read only or write','choice value of write')==null){


  g_form.addOption('variable name of read only or write','choice value of write','choice label of write');


  }


  }



}


here is my updated code, but it added read only and write as an option when Remove access is selected for type.


find_real_file.png









find_real_file.png