Email Script with .hasRole() always evaulating 'true'

Luke10
Kilo Expert

Hello,

I'm working on an email script that should include a link to the incident ticket if the user role is 'itil' and if anything else they'll be directed to the ESS portal.

My if/else statement is working. But for some reason .hasRole is always 'true'. I thought it was because I was logged in as an admin, but when I impersonate a user with no roles, I still only get a notification with the incident link.

Can anyone spot what I'm getting wrong?

Thanks!

	var currentUser = gs.getUser();
	var link = current.getLink();
	
	template.print(currentUser.hasRole('itil'));
	if (currentUser.hasRole('itil')) {
		template.print('<a href="' + link + '">GO TO INCIDENT</a>');
	}
	else {
		template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
	}
1 ACCEPTED SOLUTION

just to be clear you want to show go to Incident only when caller_id user has itil role?

var currentUser = gs.getUser().getUserByID(current.caller_id).hasRole('itil');
var link = current.getLink();

template.print(currentUser);
if (currentUser){
	template.print('<a href="' + link + '">GO TO INCIDENT</a>');
}else{
	template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
}

if you want to get other field than you need to change current.u_requested_for in belwo code.

var currentUser = gs.getUser().getUserByID(current.u_requested_for).hasRole('itil');
var link = current.getLink();

template.print(currentUser);
if (currentUser){
	template.print('<a href="' + link + '">GO TO INCIDENT</a>');
}else{
	template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
}

View solution in original post

16 REPLIES 16

Omkar Mone
Mega Sage

Hi 

Have you tried withcurrentUser.hasRolesExactly('role_name') ?

I tried that. Then it becomes 'undefined'.

Dylan Mann1
Giga Guru

How are you testing this? hasRoleExactly() is a client side method so you won't be able to use it here. You may need to simulate the function by querying the sys_user_has_role table and check if there is a record where the current user has this role and inherited is false, something like:

var currentUser = gs.getUser();
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', currentUser);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469'); // ITIL Role Sys Id
gr.addQuery('inherited', false);
gr.query();

if(gr.next()) {
   // has itil role logic
} else {
   // no itil role logic
}

I've been testing by adding the email script to a working notification. Then using the preview to see the body. I change to different INC that have been opened by different users with both roles.

I tried your code, which is a big help, but now I'm always getting a 'false' response. I think I'm missing something. Not sure what, though.

        var currentUser = gs.getUser();
	var link = current.getLink();
	var gr = new GlideRecord('sys_user_has_role');
	gr.addQuery('user', currentUser);
	gr.addQuery('role', '282bf1fac6112285017366cb5f867469'); // ITIL Role Sys Id
	gr.addQuery('inherited', false);
	gr.query();
	
	if(gr.next()) {
		template.print('<a href="' + link + '">GO TO INCIDENT</a>');
	} else {
		template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
	}