gs.getUserID() not showing current user instead its showing "system" for every user

sushilkumar
Giga Contributor

I have an email script that checks the current logged in user's role and then generates a link based on the role.

 If the user has sn_hr_core.basic role, the user will be redirected to the native view HR case.

If the user is an ESS user(No itil or HR role) it will be redirected to the ESS Portal view.

Below is the script: 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
//var instance_name = gs.getProperty('glide.servlet.uri');
var link ="";
var currentUser = gs.getUserID();
gs.info("currentUser1+"+currentUser); // showing as system for every user
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', currentUser);
gr.addQuery('role', 'f25370019f22120047a2d126c42e7061'); //checks sn_hr_core.basic
gr.query();
if(gr.next()) {
gs.info("hr");
link = gs.getProperty("glide.servlet.uri") + 'sn_hr_core_case.do?sys_id=' + current.sys_id;
template.print('<strong> <a href="' + link + '">' + current.number + '</a> </strong>\n');
}
else{
gs.info ("ess");
link = gs.getProperty("glide.servlet.uri") + 'hr?id=ticket&table=sn_hr_core_case&sys_id=' + current.sys_id + '&view=sp#hr';
template.print('<strong> <a href="' + link + '">' + current.number + '</a> </strong>\n');
}
})(current, template, email, email_action, event);

For some reason it's not getting the correct current logged in user's value. as gs.getUserID() not showing current user instead its showing "system".

Any thoughts would be much appreciated !

10 REPLIES 10

Tony Chatfield1
Kilo Patron

Hi, I think you will find that email messages are created by system and not by a logged in user; as there is no user interaction and the emails are processed in the background. There are a few exceptions, quick messages and email generated by scheduled job\report etc will show the user\run-as user as creator, but otherwise I belive there is not interactive function.

As you have access to 'current' you could look up the sys_user record via the content of 'sys_updated_by' and derive your user this way.

Hi Tony,

Our requirement is not tied to url redirection to only requestor, it needs to be dynamic if user is end user then redirect to portal view of ticket and if a fullfiller user/HR user clicks the same link then link should redirect to native view of the ticket. So i can't only search updated by or created by user for setting the url. Any suggestion would be appreciated for implementing the same.

Regards,

Sushil

Hi, I have to say I'm a bit confused if your logged in use has not triggered the email then how do you have a relationship between this user and the email message that is being triggered? Rather than just covering your email script perhaps you could provide a clear description of the end to end process so that scenario is clear?

Hi Tony,

The requirement is if User receives an email and if he tries to click on the hyperlink in the email - Based on his role page should open:

a) If an end user opens this it should redirect to portal view of the ticket

b) If user with hr basic role opens then native view of the ticket should get opened

So instead of implementing it through email script can we use SPEntryPage script include enhancement to control the dynamic redirections.