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

Mike Allen
Mega Sage

Impersonations are kept in the logs.   Is there a specific reason you need it?



Capture.PNG


Yes, I need to use it in a ACL script.


Hi Chuck,



gs.getUserId() should work.



He goes an example impersonating through script and proving that it works:



gs.print('This is my UserID:' + gs.getUserID());



var imp = new GlideImpersonate();


// Enter below the ID of the user you will like to impersonate


var previousUserId = imp.impersonate('014457ec4f678a0026afb1828110c7fa');



gs.print('This is the other person UserID:' + gs.getUserID());



if (previousUserId) imp.impersonate(previousUserId);


gs.print('This is my UserID:' + gs.getUserID());



Thanks,


Berny


You can run this script from a background script using elevated privileges.



The output will be something like the following:



*** Script: This is my UserID:xxxx083c4fc0920026afb1828110xxxx


*** Script: This is the other person UserID:014457ec4f678a0026afb1828110c7fa


*** Script: This is my UserID:xxxx083c4fc0920026afb1828110xxxx



Thanks,


Berny