- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 04:39 AM
I am new to the ServiceNow environment and need some help with a client script and script include that I am trying to write and obtain the correct output.
I am trying to write a script that will display certain values of the state field on the incident form depending on the user’s role.
Here is my script. I would appreciate any feedback.
//CLIENT SCRIPT
//Remove options: Resolved, Closed, or Canceled if user has the
//Incident Modified role.
function onLoad() {
var ga = new GlideAjax('RoleandState'); //RoleandState is the script include name
ga.addParam('sysparm_name','RoleandState'); // Calling the RoleandState function
ga.addParam('sysparm_user_role',' Incident Modify',' Incident Resolve', ' Incident Close'); //Role values seeking
ga.getXML(RoleandStateParse); //Calling the server function with responsex.query();
function RoleandStateParse(response) {
if (x.role == ' Incident Modify' && ('state' != 6 ||'state' != 7 || 'state' != 8)){
//User has Incident Modify
g_form.removeOption('state', '6'); //Resolved
g_form.removeOption('state', '7'); //Closed
g_form.removeOption('state', '8'); //Canceled
if (x.role == ' Incident Resolve' || x.role == 'Incident Close' && ('state' != 6 ||'state' != 7 || 'state' != 8)){ //User has Incident Resolve or Close roles
g_form.addOption('state', '6');
g_form.addOption('state', '7');
g_form.addOption('state', '8');
if (x.role == ' Incident Modify' && x.role == 'Incident Resolve' && ('state' != 6 ||'state' != 7 || 'state' != 8)){ //User has Incident Modify and Resolve roles
g_form.addOption('state', '6');
g_form.addOption('state', '7');
g_form.addOption('state', '8');
if (x.role == ' Incident Modify' && x.role == 'Incident Close' && ('state' != 6 ||'state' != 7 || 'state' != 8)){ //User has Incident MOdify and Close roles
g_form.addOption('state', '6');
g_form.addOption('state', '7');
g_form.addOption('state', '8');
if (x.role == ' Incident Modify' && x.role == 'Incident Resolve' && x.role == ' Incident Close' && ('state' != 6 ||'state' != 7 || 'state' != 8)){ //User has Incident MOdify, Resolve and Close roles
g_form.addOption('state', '6');
g_form.addOption('state', '7');
g_form.addOption('state', '8'); }
}
}
}}}}
//SCRIPT INCLUDE
var RoleandState = Class.create(); //script include name
RoleandState.prototype = Object.extendsObject(AbstractAjaxProcessor, {
RoleandState: function() { //function called from client script
var role= []; //this.getParameter('sysparm_name', 'RoleandState'); //input received from client
var x = new GlideRecord('sys_user_role');
var m = g_user.userID;
x.addQuery('user', m);
x.addquery('role', x);
x.query();
while (x.next())
{
return role;
}
},
type: 'RoleandStateParse'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2019 08:34 PM
Hi MR,
Did you manage to test my revised solution?
If it worked for you then please mark as correct so other users can benefit from this information.
Thanks in advance.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 01:07 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 01:59 PM
g_user is a client side object. You cannot use it in a script include which is server side code. You do not need a GlideAjax here, you have g_user.hasRole('role_name goes here') method to check the users's role. You can do this check in your client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 02:26 PM
Hi Ya,
As Juan and abhinay pointed out, g_user is available from the client side which means you won't need to make a call to the server.
I think we can simplify the code somewhat by using g_user.hasRoleFromList which checks the users roles against a comma-separated list passed as a parameter.
Code:
function onLoad() {
//Type appropriate comment here, and begin script below
var hasRequiredRole = g_user.hasRoleFromList("Incident Resolve, Incident Close");
var incModRole = g_user.hasRole("Incident Modify");
var curState = g_form.getValue("state");
//if the user has the "Incident Resolve" or "Incident Close" roles and the state is not Resolved, Closed or Canceled.
if (hasRequiredRole && (curState != 6 || curState != 7 || curState != 8)) {
g_form.addOption("state", "6");
g_form.addOption("state", "7");
g_form.addOption("state", "8");
}
//all other roles will have these options removed.
else {
//If you need to apply this logic to "Incident Modify" role only then replace "else" with "else if (incModRole)"
g_form.removeOption("state", "6");
g_form.removeOption("state", "7");
g_form.removeOption("state", "8");
}
}
Let me know if this worked for you.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 04:56 AM
Thanks Brent, the script works until an additional role is added to the user's profile.
For example, the user only has Incident Modify. The proper values are listed in the state field. If I go and add the Incident Modify or Incident Close to the user's profile, it will not display the additional options in the 'state' field (Closed/Resolved/Canceled).
Thanks again for all your assistance