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

Krushna R Birla
Kilo Sage

Hi @Gemma4 

 

You have to use field name whenever you used getValue. Like, g_form.getValue(<field_name>);

In your case, it should be g_form.getValue('request_state');

 

Coming on to the point, to remove specific request state from choice field, you have to use removeOption method and add in conditional statement, Like 

g_form. removeOption("<field_name>", "<option_to_be_removed>"); 

 

Here is your Solution:-

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

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

 

I tried the following but it is removing closed skipped from all roles and not just pm

 

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

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

Thank you so much. Using hasRoleExactly, resolved the issue. Appreciate all the feedback!