How do I get the sys_id of the impersonating user name?

chucksimonds
Kilo Expert

I want/need to get the sys_id of the person impersonating, like getUserID(), but the ID of the person doing the impersonating.

Was hoping there was something like:   getImpersonatingUserID(); but doesn't exist.

I tried this...     var u = gs.getImpersonatingUserName().gs.getUserID();

but it did not work. Any help is appreciated, thanks!

1 ACCEPTED SOLUTION

Hi Chuck,



got it.



You can basically do a simple get with the UserID of the impersonating user - that query is pretty lightweight and shouldn't affect performance too much.


Then you can simply add the sys_id of that person to your query.



You might want to verify if the impersonatingUserSysID is set/filled - if not don't even include it in the answer line (so having two answer lines - one for the case that you have the id and for the case that you don't).



var impersonatingUserName = gs.getImpersonatingUserName();


var impersonatingUserSysID = "";



var grUser = new GlideRecord("sys_user");


if(grUser.get("user_name", impersonatingUserName)) {


        impersonatingUserSysID = grUser.getValue("sys_id");


}


View solution in original post

24 REPLIES 24

fschuster
ServiceNow Employee
ServiceNow Employee

Chuck,



as far as I know there's no function in the core code that would return the sys_id of the impersonating user.


So either way you would have to do a query with the user id to get the sys_id of that user.



What exactly do you want to achieve?



Best


Frank


Hi Frank, here is basically what I'm trying to achieve, this works for a user not impersonating:


u = gs.getUserID();


answer = current.caller == u || current.opened_for == u || current.opened_by == u || current.watch_list.toString().indexOf(u) >=0 || current.isNewRecord() || !current.getUniqueValue();



When a user is impersonating someone, the getUserID retrieves the sys_id for the person I'm impersonating, not me. To limit access to my records, I need to get my sys_id in all instances, whether I'm impersonating someone or not.


If I need to use a query, what would the query look like. I need the parameters, as I am not a script coder. That's why I was hoping for an easier one line script like getImpersonatingUserID. I don't know why they have one to get the impersonator's display name and user name, but not one to gt their sys id.



Thanks!


Hi Chuck,



got it.



You can basically do a simple get with the UserID of the impersonating user - that query is pretty lightweight and shouldn't affect performance too much.


Then you can simply add the sys_id of that person to your query.



You might want to verify if the impersonatingUserSysID is set/filled - if not don't even include it in the answer line (so having two answer lines - one for the case that you have the id and for the case that you don't).



var impersonatingUserName = gs.getImpersonatingUserName();


var impersonatingUserSysID = "";



var grUser = new GlideRecord("sys_user");


if(grUser.get("user_name", impersonatingUserName)) {


        impersonatingUserSysID = grUser.getValue("sys_id");


}


Frank, that's exactly what I need. Was hoping for a 1 or 2 liner, but I only perform the query if an impersonator is found. The only thing now is that I'm seeing a lot of script warning messages in the system log. About 50 lines or more and it shows for every record it finds for this user. Maybe I can turn off script warnings or something. Any ideas? I will mark this as correct answer at some point unless I get a better 1 line script (lol)! Thanks again for your help!


So the simplest way would be to use:



(gs.getImpersonatingUserName() === null)



...as part of your condition.   Basically, do not allow access if someone is impersonating another user.   "getImpersonatingUserName()" will return the impersonating user's name, so you can tell if it is the real user if it returns null.



Why bother checking to see if the impersonator is looking at their own records?   Why would they be?