Hiding an option on a multiple choice field.

Mike Pottorf
Tera Contributor

I am trying to hide one of the options on a multiple choice field, on record producer, dependent on who opens the form.   I have tried using a catalog client script as well as scripting with UI policies with no luck.   I have been using g_form.removeOption, g_form.setVisible, and g_form.addOption in my attempts to hide the option. My first question would be, is this even possible based upon who is opening the form?   If so, what is the best way about scripting it, client or UI policy?   And lastly, what scripts would I use to hide the value?   Thanks for your help.   Any suggestions would be helpful.

Thanks,

Mike

9 REPLIES 9

It know this is relatively old but I'm looking for the same solution.


I tried that script and it works but the IDs are for the question and not for the individual selection, at least the way ServiceNow outputs it. In my case, I have a multiple choice with 3 options. The ID matches the question so it only removes the first choice. Tried the sys_id of the answer but that breaks the form rules.



Would there be any easy way to remove the 2nd or 3rd choice?


You should be able to using other DOM element objects.These are only ideas (I don't have time to test), but you could try:


var c = document.getElementById("Label ID").childNodes; // might need another childNodes


if (c[1]){


  c[1].style.visibility="hidden";


  c[1].style.display="none";


  }


}



or



var y = x.getElementsByTagName("Value");


var i;


for (i = 0; i < y.length; i++) {


if (y[i].indexOf("Delete Asset Only") > 0) {


  y[1].style.visibility="hidden";


  y[1].style.display="none";


  }


}


Thanks for the reply. I don't think that works properly though. It doesn't like the c[1] var in the code.


On top of that, I'd need to figure out how to limit it by the user criteria information in Fuji which I'm not too sure on how to implement in a script.


Hi Tim, I made some progress using your suggestion however it looks all of my IO are identical for all my select choice options according to inspector in my browser. I am also not used to working this, am I not catching something correctly?



<input data-original-title="Change Access / Ownership" id="IO:cab073442bafb100963b26e405da15e7" name="IO:cab073442bafb100963b26e405da15e7" class="cat_item_option" onclick="if (typeof(variableOnChange) == 'function') variableOnChange('IO:cab073442bafb100963b26e405da15e7')" title="" value="Change User" type="radio">



<input data-original-title="Terminate / Remove" id="IO:cab073442bafb100963b26e405da15e7" name="IO:cab073442bafb100963b26e405da15e7" class="cat_item_option" onclick="if (typeof(variableOnChange) == 'function') variableOnChange('IO:cab073442bafb100963b26e405da15e7')" title="" value="Delete User" type="radio">


Instead of trying to find the options by order or id in the array, it was easier for me to just compare the innerHTML of the option. I suppose you could just compare the actual value, but the inner text works for us.



// Get the options into an array


var options = document.getElementById("my_table_name.my_state_field").childNodes;


var temp = "";


for (i = 0; i < options.length; i++) {


  temp = options[i].innerHTML;


//if the option in the dropdown list is 'Closed' or 'Cancelled' then hide it


  if (temp == "Closed" || temp == "Cancelled") {


  options[i].style.display = "none";


  }



I will have another script that will overwrite the display= "none" when certain conditions are met. I found the id of the select field using the developer tools in Chrome. The childNodes part returns the array of options, for those who have less experience in scripting.