How to Remove Choice values based on Roles in Client script

Manasa23
Tera Contributor

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

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so what debugging you performed?

did you add any alert etc?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Abhijit4
Mega Sage

Hi Manasa,

What is your current output and what are you expecting?

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi,

           refer below link it may help you : https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options/

Aman Kumar S
Kilo Patron

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.

Best Regards
Aman Kumar