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

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!

Thanks for marking my reply as Helpful.

Not sure if it was as simple as a typo, haha...but to add an option, appropriately, you'd want to add more than just the value. Please refer to my reply for assistance with that.

 

In any case, if my reply helped guide you towards a resolution, please also mark my reply as "Accept Solution". You can mark more than one as Accept Solution too, if that applies.


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

Hi Allen,

I did change it to be a toString, but that gives me a 'null' answer, so I swapped it back. I did change the add option to be:

 

 

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

 

 I am still getting the same results, however.

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!