The CreatorCon Call for Content is officially open! Get started here.

How to hide Choice options without removing them

ritaaudi
Tera Contributor

Hi: I want to be able to hide some Choice entries but not remove them, so that they can be updated by business rules but prevent the user from selecting them. Is there a way to do this? I know you can use g_form.removeOption(<field>, <choice>). I guess I am looking for a g_form.hideOption(<field>, <choice>), but that doesn't work.

Thanks for your help!

Rita

1 ACCEPTED SOLUTION

Sure Rita.



Just to give you another option-


"g_form.removeOption(<field>, <choice>) did not work for me because when I set the removed choice using the business rule, the removed choice did not appear on the form."


To show the removed choice when it is set by business rule you can add condition to the onLoad client script/UI action(containing removeOption()) to check if the current choice is not the one to be removed and if it not then remove the option.


For e.g. lets say you don't want to allow the users to select state1.


Then you can add condition - State is not state1 - and in script 'g_form.removeOption('state','state1');'.


This will remove the option only if it is not currently set on the record. If its set then it will be displayed else it will be removed. If you again set it to any other state again it will be removed.



--Mark correct/helpful if it helps solving your issue.


View solution in original post

12 REPLIES 12

Edvin-Karalius - Your reply is nearly what I seek but given my list of choices or or options to hide is considerably longer than choices always visible.  I need the inverse where onload it has list of always available options and to that will be appended the current option of the loaded record if not one of those always available. The remainder of available choices need to be removed, not just disabled.  I have not got it to work just yet so while I continue to peck at it, help appreciated.  Thank you.
Details:
Choice field (u_source_of_change) has 10 choices with 3 always allowed in UI ('change', 'incident', 'problem'), and 7 others (and growing) that do not appear in choice field dropdown (not just grayed out) that are used by integration services and cannot be selected when creating new Change record using the UI.  Updates to u_source_of_change when it is one of those hidden choices include to one of the 3 always allowed or to remain as is.  If none of the hidden choices are used (examples, its empty as new record create in UI or already an allowed value for example 'change' has been selected) when UI loads, those 7 hidden choices cannot be selected because they do not appear.  For example, value is 'Email' then updated in UI to 'Change', update is then saved, now it cannot go back to 'Email').   Today I have a CS to remove each hidden choice but it would be far easier to not have to edit CS every time we add another hidden choice.  An alternate approach I imagine is at onload, disable all options then enable those always allowed plus the current used in record.  Then there are no loops to script but again, not as elegant as your code sample.  Anyway all the best.

I'd say you should have all the options available in the choice list as the default approach.
Reason being is, for integrations it's even better if you use something like GlideQuery that can validate the options. So that the API won't insert values that are not allowed.

Second, yes, in the CS you can define the 3 available states on the client. + current state.
Then you remove all options except the 3 + current.

You can also use a query if you dont want to add every field manually. I did this with AJAX because I already have a util script for this, but u can do it also in client script.

 

CLIENT SIDE

function onLoad() {
   var options= Array.from(g_form.getControl('cause_code')); //Set your target field
   var query = 'nameSTARTSWITHproblem_task^elementSTARTSWITHcause_code^sequence<100^language='; // set query, language will be set on AJAX
   var ga = new GlideAjax('global.choiceUtil');
   ga.addParam('sysparm_name', 'removeOptions');
   ga.addParam('sysparm_query', query);
   ga.getXML(function(response) {
		var hiddenOptions = [];
		var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
		if (answer["status"] == "ok") {
		hiddenOptions = answer["hiddenOptions"];
	// remove filtered options from field

   options.forEach(function(e){
	if(hiddenOptions.indexOf(e.value) > -1)
		e.hidden = true;
		});
   }});
}

SERVER SIDE

  removeOptions: function(){
	var query = this.getParameter('sysparm_query');
	var result = {
    "status": "error",
	"hiddenOptions": [],
    };
	var lang = gs.getSession().getLanguage();
	var hiddenOptions1 = [];
   var hide = new GlideRecord('sys_choice');
   hide.addEncodedQuery(query+lang);
   hide.query();
   while(hide.next()){
	hiddenOptions1.push(hide.value.toString());
   }
	result["status"] = "ok";
	result["hiddenOptions"] = hiddenOptions1;
   return JSON.stringify(result);

  },