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

Ahmmed Ali
Mega Sage

How this emails are getting triggered? From event? if yes, then try passing current user sys_id in parm2 of the event and in script include query sys_user_has_role table with that sys_id.

 

because some of the process in SNOW are processed using system as user instead of logged in user. for example in workflow, after timer activity the updates will be tagged as updated by system. same with events and notification.

 

I suspect this might be the issue.

 

just to validate add log statement to print current logged in user:

gs.log(gs.getUserName(),"Notification_user"); // check logs with source as Notification_user

 

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

This is for incident notifications. So, the email script will be added to the notifications for incident. Which may an issue when it comes to testing.