Impersonating users through script

JimmieOdelius
Tera Expert

Hi.


I'm setting up tests in the Automated Test Framework and I ran into trouble when I stumbled upon the need to impersonate users via script.

I found somewhere else on the community that I could use

session.setUser("username");

This did not work however and resulted in an error message like "Attempted script access to inaccessible member denied." which my google-fu failed to find anything about.

Has anyone done anything like this before? Please help.

Thanks in advance.

1 ACCEPTED SOLUTION

Brandon Smith3
Kilo Expert

Jimmie,



I've had little luck with impersonations, however I have used gs.getSession().impersonate('xxx') within an ajax script include. Give it a try, hope it helps!


View solution in original post

7 REPLIES 7

dravvyramlochun
ServiceNow Employee
ServiceNow Employee

Hello Jimmie,



The below is a script that I use to impersonate a user and run a BR, maybe this can put you on the correct path:



   


session.onlineImpersonate("abel.tuter");


var current = GlideRecord('incident');


if (!gs.hasRole("itil")) {


  var u = gs.getUserID();


  var qc = current.addQuery("caller_id", u).addOrCondition("opened_by", u).addOrCondition("watch_list", "CONTAINS", u);


  gs.print("query restricted to user: " + u);


}


current.query();


while (current.next()) {


gs.print(current.number);


}


session.onlineUnimpersonate();


Another script that I used to test if my impersonation works is as below:



var og = gs.getSession().impersonate('5136503cc611227c0183e96598c4f706');


gs.print(gs.getUser().getFullName());



var gr1 = new GlideRecord('sn_customerservice_case');


gr1.addQuery('sys_id',b08cd51fdba83a007a4cbedffe96196d');


gr1.query();


if(gr1.next())


gs.print("Found it");


else


gs.print("Not Found");



gs.getSession().impersonate(og);


*** Script: Guest


*** Script: Not Found


Priyanka95
Kilo Contributor

I have tried all of them but its not working in my case.

My case: I am using this in a runscript of a workflow where I am trying to update one of the sctask of the request item with the impersonated user. But the update is happening with the user who raised the request. And if I update the request item, it is updated with impersonated user.

So the issue is when I am gliding the sctask that was already created and updating the record.

Is there something I am missing. PLease help.

I have tried gs.getSession().onlineImpersonate("<user_name>");,new GlideImpersonate().impersonate(your user id);etc

The impersonate log says it is sussesfull but still the update is not with impersonated user.