Hide Request States based on user role

Gemma4
Mega Sage

Hi everyone,

I have a requirement to hide a few request.states (not state) based on user role pm. Below are a couple client scripts I've attempted with no luck. The first script example is a copy of (BP) Hide Choice - Closed from incident table. Does anyone have any suggestions? 

Thanks in advance for any feedback you can provide!

 

 

example 1

 

// Hide "Closed" Incident state and State from everyone but itil_admin
//This is a copy of Client script (BP) Hide Choice - Closed-This script should hide Closed skipped, Closed Incomplete, and Pending Approval for users with PM role
 
 
function onLoad() {
if (g_user.hasRole('itil_admin'))
return;
 
// if (g_form.getValue('incident_state') != '7') {
if (g_form.getValue('sc_request_request_state') != 'closed_skipped') {
// if (typeof g_form.getOption === 'function' && g_form.getOption('incident_state', '7'))
if (typeof g_form.getOption === 'function' && g_form.getOption('sc_request_request_state', 'closed_skipped'))
// global_var_choice_display_values.incident_state = g_form.getOption('incident_state', '7').text;
global_var_choice_display_values.incident_state = g_form.getOption('sc_request_request_state', 'closed_skipped').text;
// g_form.removeOption('incident_state', 7);
g_form.removeOption('sc_request_request_state', closed_skipped);
}
// if (g_form.getValue('state') != '7') {
if (g_form.getValue('request_state') != 'closed_skipped') {
// if (typeof g_form.getOption === 'function' && g_form.getOption('state', '7'))
if (typeof g_form.getOption === 'function' && g_form.getOption('request_state', 'closed_skipped'))
//global_var_choice_display_values.state = g_form.getOption('state', '7').text;
global_var_choice_display_values.state = g_form.getOption('request_state', 'closed_skipped').text;
// g_form.removeOption('state', 7);
g_form.removeOption('request_state', closed_skipped);
}
}
 
var global_var_choice_display_values = {
//incident_state: "",
sc_request_request_state: "",
// state: ""
request_state: ""
};
 
 
2nd client script tried
 
function onLoad() {
   //Type appropriate comment here, and begin script below
if(g_user.hasRole("pm"))
return true;
g_form.setDisplay('closed_skipped', false);
   g_form.setDisplay('closed_incomplete', false);
 
return false;
}
 
 

 

 

1 ACCEPTED SOLUTION

It seems your are testing with admin user. To test whether the currently logged in user has the role "pm" explicitly assigned, use the hasRoleExactly() method

 

Please use below code and let me know,

function onLoad() {
    if (g_user.hasRoleExactly("pm")) {
        g_form.removeOption("request_state", "closed_skipped");
    }
}
 
It should work now. hasRoleExactly, returns true only if the current user has the specified role "pm"
 

Please mark my answer as helpful and accept it as the solution if it serves your purpose.

 

Thanks and Regards,
Krushna Birla
ServiceNow Consultant
LinkedIn

View solution in original post

8 REPLIES 8

Community Alums
Not applicable

Hi,

 

if (g_form.getValue('sc_request_request_state') != 'closed_skipped') {
 
Is the "sc_request_request_state" field name passed correctly in the above line? Pass the field name correctly and check.

thank you for the feedback. I think I passed it correctly, but I'm not 100% as this is not my specialty. The table is sc_request and the field name is request request_state

Community Alums
Not applicable

What exactly are you trying to achieve here? Are the client scripts written on Incident table or Request table?

The client script is written on the request table. All I want to do is hide a few request states if a user has a role pm. I can hide the states with a very basic client script below. However, when I add if the user has pm role the script doesn't hide it just for those particular users. The script below is hiding the states for everyone and it just needs to hide it for users with pm role. Hope that helps to explain better. thanks again for all the feedback 

 

function onLoad() {
//   if (g_user.hasRole("pm"))
// return;
g_form.removeOption('request_state', 'closed_skipped');
g_form.removeOption('request_state', 'closed_incomplete');
}