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

Hi, I don't believe that SPEntryPage will help because of the url/link structure, and I don't think you can achieve this behaviour dynamically as an email may have multiple recipients. I had a similar scenario and just included 2 seperate links in my email messages - but the solution is not ideal. If these are emails to single users ie incident 'caller' you could use either an email script that looks up the user record and creates the url based on their role (similar to your current direction); Or a br to check the incident.caller_id sys_user role(s) and trigger one of 2 sys_events that in turn trigger a marching notification. If recipients are multiple and the users in a group or watch list you could fire 1 sys_event that triggered 2 notifications(one for each url) and use an email script to populate (or drop) users from each url specific notification based on the role lookup.

Sandesh9
Tera Guru

Hi sushil,

instead of querying the user table can you try using gs.getUser().hasRole("sn_hr_core.basic"). This should be more efficient and also will make your code compact. 

Hope this helps. Mark helpful or correct based on the impact

Hi Sandesh,

 

Now I tried below script but still getting the same error. As gs.user() is not able still pick correct logged in user.

 

var link ="";
var currentUser = gs.getUser().hasRole("sn_hr_core.basic");
gs.info("currentUser2+"+currentUser); //showing as false for ever user logged in user even if user has roles.
gs.info("currentUser3+"+gs.getUser()); // getting output:: currentUser3+com.glide.script.fencing.ScopedUser@1586790

if(currentUser){
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');
}

 

asifnoor
Kilo Patron

Hello Sushil,

Notification runs in the background and has nothing to do with the logged in user. The logged in user information is available at the place which is triggering this notification.

So You need to pass the logged in user data during the triggering of the notification (which could be during insert/update of the data) and then capture that user in the mail script.

Mark the comment as a correct answer and also helpful if it answers your question.

Vikas-Malhotra
Mega Guru

Hi Sushil,

 

You will have to change you logic in order to get the required condition using the fields on the table "sn_hr_core_case" itslef.

 

You won't be able to user gs.getUserID() in an email script.

 

Thanks!