Catalog Client Script Hide / Add Choice on Variable

Julia Howells
Giga Guru

Hi all,

I have a catalog client script that is checking if the logged in user has a certain type listed on the sys_user profile. 

My alert is firing and shows the correct answer, but it is not doing the next steps. 

 

I know my Script Include is working, or I wouldn't be getting the answer. Any ideas on how to get the req_type to show or hide 'add' when the sys_user type is not one of the choices?

function onLoad() {
		
    var ga = new GlideAjax("global.ClientUserUtils");
    ga.addParam('sysparm_name', 'getPrimaryAffiliate');
    ga.getXML(gaResponse);

	function gaResponse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		alert('What is the type: ' + answer);
		if (answer == 'Choice 1' || answer == 'Choice 2' || answer =='Choice 3') {
        g_form.addOption('eq_type', 'add'); // this is not working
    } else {
        g_form.removeOption('req_type', 'add'); // this is not working

    }
}
}

 

2 ACCEPTED SOLUTIONS

Allen Andreas
Administrator
Administrator

Hi,

Is the "answer" in string and it equates to one of those selections? Choice 1, Choice 2, or Choice 3?

You may want to use  typeof answer in your alert as well and see if it's string or not, if not, set it to string and then see how your script works?

 

When adding an option, you need three values:
g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);

the last one, index, is technically optional.

 

For removing it's: g_form.removeOption(<fieldName>, <choiceValue>);

 

Documentation for more assistance: https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

Hi,

It would be toString() - just incase you weren't using it that way. And please check your actual field name. Is it really req_type?

Anyways, for the addOption, please refer to my reply above. The format is:

g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);

Such as...

g_form.addOption('req_type', 'add', 'Add');

The 4th parameter is if you want to specify it being in the 1st spot, 2nd spot, 3rd spot, etc. in the choice list. Such as:

g_form.addOption('req_type', 'add', 'Add', 1);

 

This all only works on a choice list/select-box.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

5 REPLIES 5

J_31
Kilo Sage

I think there is  a typo in addoption - 'eq_type'

 

try this code:

 

function onLoad() {

var ga = new GlideAjax("global.ClientUserUtils");
ga.addParam('sysparm_name', 'getPrimaryAffiliate');
ga.getXML(gaResponse);

function gaResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('What is the type: ' + answer);
if (answer == 'Choice 1' || answer == 'Choice 2' || answer =='Choice 3') {
g_form.addOption('req_type', 'Choice 4'); // add the option with value 'Choice 4'
g_form.removeOption('req_type', 'add'); // remove the option with value 'add'
} else {
g_form.addOption('req_type', 'add'); // add the option with value 'add'
g_form.removeOption('req_type', 'Choice 4'); // remove the option with value 'Choice 4'
}
}
}