Create and set a current user session
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2017 01:07 PM
I have a scripted REST API where I want to create a new user session based off the user name (line 4) in the request header. However, because this integration does not use any type of authentication, the current user is identified as "guest". I want to set the current user to the user session that is created in my gliderecord query below. How can I do this?
//create a user session
var gr = new GlideRecord('sys_user_session');
gr.initialize();
gr.name = sn_user_name;
gs.info('this is the username for the user session' + sn_user_name)
gr.insert();
gs.info('current user session GlideRecordSecure query: {0}', gs.getUserID());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2018 12:31 PM
I think inserting the record into the 'sys_user_session' will not create the new user session. This will track the logged in user sessions. To create a new user session we have to use both username and password.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2018 11:08 AM
If you have not solved this I think you are going to have to use Digest auth to make this happen.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2018 06:27 AM
Use the following code to execute block of script with some user not with the guest.
var new_user = 'sys_id of user';
session.onlineImpersonate(new_user);//system will execute following script with new_user user.
<your_code>
session.onlineUnimpersonate();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2018 02:34 AM
Hi All,
I would like to do the same in a scoped app, I see that the above solution only works in a global scope.
gs.getSession().impersonate('161ce02e4f166640e11fe3414210c76b');// works in global scope but not in application scope.
https://community.servicenow.com/community?id=community_question&sys_id=66aebe65db58dbc01dcaf3231f961941
Kal