Hide/display certain values of the state field on the incident form depending on the user’s role.

MR16
Giga Expert

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'

});                                        

1 ACCEPTED SOLUTION

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. 

View solution in original post

7 REPLIES 7

Hi MR,

I forgot to add in the option description. Try the following:

function onLoad() {
   //Type appropriate comment here, and begin script below
	var hasRequiredRole = g_user.hasRoleFromList("Incident Resolve, Incident Close");
	console.log("BLS hasRequiredRole: " + hasRequiredRole);
	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('priority', '6', '6 - Really Low');
		g_form.addOption("state", "6","Resolved");
		g_form.addOption("state", "7","Closed");
		g_form.addOption("state", "8","Canceled");
	}
	//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");
	}
}

Should work now. Let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information. 

Brent Sutton
Mega Sage

Hi MR,

I forgot to add in the option description. Try the following:

function onLoad() {
   //Type appropriate comment here, and begin script below
	var hasRequiredRole = g_user.hasRoleFromList("Incident Resolve, Incident Close");
	console.log("BLS hasRequiredRole: " + hasRequiredRole);
	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","Resolved");
		g_form.addOption("state", "7","Closed");
		g_form.addOption("state", "8","Canceled");
	}
	//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");
	}
}

Should work now. Let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information. 

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.