
Alex240
Giga Expert
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-25-2022 01:42 PM
The following piece of code determines and returns the node which a specified user is logged into.
How to use:
- Replace variable username by the user ID
- Execute as background script / fix script
(function () {
var username = 'admin'; //sys_user.user_name
var userSession = new GlideRecord('v_user_session');
userSession.addQuery('user', username);
userSession.addActiveQuery();
userSession.setLimit(1);
userSession.query();
if (!userSession.next()) {
gs.log('User is not logged in');
return;
}
var sessionID = userSession.getValue('session_id');
var transactionLog = new GlideRecord('syslog_transaction');
transactionLog.addQuery('session', sessionID);
transactionLog.orderBy('sys_created_on');
transactionLog.setLimit(1);
transactionLog.query();
if (!transactionLog.next()) {
gs.log('Unknown error');
return;
}
var systemId = transactionLog.getValue('system_id');
systemId = systemId.split(':');
var node = systemId[systemId.length - 1];
gs.log(username + " is logged into node: " + node);
})();
‼️ Disclaimer: Please, always test the script in non-productive environments before running in a productive instance. Script was reviewed at the creation time but may break/get unexpected results due to instance versioning and SN API upgrades.
Labels:
- 2,157 Views
Comments
Maik Skoddow
Tera Patron
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
05-25-2022
07:41 PM
Can be done with a one-liner:
GlideServlet.getSystemID()
Maik
Aqib
Tera Contributor
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
05-26-2022
02:05 AM
The OP gets the node for a specific user, not the current user.
Surya Kuraku SN
ServiceNow Employee
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
11-08-2022
07:25 PM
@Maik Skoddow can you please provide more info on GlideServlet.getSystemID() method?