How to Remove Choice values based on Roles in Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-27-2022 11:50 PM
HI All,
I have tried the below Client script to remove & Add choice values and hide field based on Role, However it is not working.
I have written multiple IF conditions in one Function , not sure whether it is correct or not ..
can someone please help on this !!
Client script - Onload
function onLoad() {
if (g_user.hasRoleExactly('asset')) {
g_form.addOption('u_select_the_type_of_action', "create_update");
} else {
g_form.removeOption('u_select_the_type_of_action', "create_update");
}
if (g_user.hasRoleExactly('HAM_CMDB_Support')) {
g_form.addOption('u_select_the_type_of_action', 'update_hostname');
} else {
g_form.removeOption('u_select_the_type_of_action', "create_update");
}
if (g_user.hasRoleExactly('inventory_admin')) {
g_form.removeOption('u_select_the_type_of_action', 'allocation_deallocation');
g_form.setDisplay('u_select_the_type_of_asset', 'true');
} else {
g_form.removeOption('u_select_the_type_of_action', 'allocation_deallocation');
g_form.setDisplay('u_select_the_type_of_asset', 'false');
}
}
Thanks,
M
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-27-2022 11:56 PM
Hi,
so what debugging you performed?
did you add any alert etc?
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-27-2022 11:57 PM
Hi Manasa,
What is your current output and what are you expecting?
Thanks and Regards,
Abhijit
Community Rising Star 2022
Regards,
Abhijit
ServiceNow MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-28-2022 12:03 AM
Hi,
refer below link it may help you : https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-28-2022 12:04 AM
Hey,
Few suggestions here:
1. hasRoleExactly will work only if user has that one role, for example has both itil and asset, this check will fail for hasRoleExactly('asset')
2. For addOption:
g_form.addOption('u_select_the_type_of_action', "create_update"); // this is incorrect
Use this:
g_form.addOption('u_select_the_type_of_action', "create_update", "Create Update"); // last parameter for label
I would rather suggest, if you can pass use detail to script include using glideajax and get your query done there.
Aman Kumar