Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Mike Patel
Tera Sage

try below. You cannot user preview in notification since it will grab logged in user role. 

var currentUser = gs.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>');
}

I tied this, too. It always evaluates to 'true'. And I tested by impersonating a non-itil user and actually viewing the email sent. Which I've been doing also before now. Maybe I'm testing this wrong?

try below if you want to get role of requested_for user

 

var currentUser = gs.getUser().getUserByID(current.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>');
}

Tried this, too. I keep getting false even if they have the itil role.

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>');
}