OnLoad g_form.removeOption not working

Maria20
Tera Guru

Hello SN Developers!

I have an onLoad event that is supposed to hide an option on a field if the user does not have a specific role (and conversely, add the option if they do).  I know the code is firing correctly because of the alerts I put in but the option is not working as expected. 

Here's my code:

Maria20_0-1666186196272.png

Any thoughts/suggestions?

1 ACCEPTED SOLUTION

@Maria20, yes thats actually the issue. The removeOption function does not work for reference fields. There you will have to use „reference qualifiers“ or ACL rules. 

Greetings, 

Sören

View solution in original post

7 REPLIES 7

Soeren Maucher
Mega Sage

Hello @Maria20

in the function "removeOption", you have to provide the field name and then the option value. 
Could it be that you are currently using the option Text instead of the Option Value (highlighted in yellow in th following Screenshot). 

I reproduced your situation and it is working for me: 

SoerenMaucher_0-1666187559850.png

 

SoerenMaucher_1-1666187587505.png


Furthermore, I would suggest to either hide or remove the option depending on the role. You dont have to do both. This you would only need if the user changes his role between two loads, which would probably not happen. In my experience adding an option can sometimes lead to unexpected behavior, so I would suggest just removing an option if the user does not have the mentioned role. 

I hope this helped! 

Greetings,
Sören




Musab Rasheed
Tera Sage
Tera Sage

Hello,

What Soren said is correct, you can just add removeOption and no need to add Addoption again.

https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options...

Please hit like and mark my response as correct if that helps
Regards,
Musab

Hello Musab,

Maybe my problem is that my "application" field is not a choice list but a reference-type field?!?!

Maria.

@Maria20, yes thats actually the issue. The removeOption function does not work for reference fields. There you will have to use „reference qualifiers“ or ACL rules. 

Greetings, 

Sören