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

Ah-ha. So, it works with current.caller_id. I think there may have been some confusion because the label say 'Requested for', but its field name in caller_id. But getting the user id then checking for the 'itil' role fixed the issue. Thanks, Mike!

Jim Coyne
Kilo Patron

Few question:

- How/when does it go out?
- What is the content of the email?
- Who does it go to?

The reason I ask is the roles the current user has may be irrelevant to the email.  I may trigger the email to go out to someone else, so whether I have "itil" or not should not matter.  It is the recipient that matters.

Take a look at the "Incident commented for ITIL" and "Incident commented for ESS" Notifications as examples. They are different because the audiences are different, which I believe is what you are looking for.

It goes out with incident notifications. So, this email script will either provide the 'ticket link' or 'ESS link' with each notification. It should go to the 'requested for'.

In that case, we don't want to use gs.getUser() you'll want to check if current.requested_for has the ITIL role. 

You can try this:

var isITIL = current.requested_for.hasRole('itil');

if(isITIL) {
   // has itil role logic
} else {
   // no itil role logic
}

If that doesn't work, or you want it to be more like hasRoleExactly() you can try this:

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.requested_for);
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
}

Gave this a try. Ended up with role being undefined each time.