gs.getUserID() not showing current user instead its showing "system" for every user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 04:20 PM
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 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 10:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 05:43 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 07:16 PM
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');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 11:31 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 11:46 PM
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!