How to check user is impersonated using script?

rahulyamgar
Tera Guru

Hello friends, 

 

I was trying to impersonate with certain user using the Glide impersonation APIs using below script. However, I was not able to check in background script if current session is impersonation or not.

 Below returned FALSE in every gs.info statement, whereas if same script is executed by externally impersonating i.e. through UI, it returned TRUE always. Any thoughts?

 

gs.info('FROM BACKGROUND SCRIPT1'+GlideImpersonate().isImpersonating());

gs.info('******************************************************************************');
//gs.print ("The current user name is: " + user);
var currentUser = gs.getUserID();
var impUser = new GlideImpersonate();
impUser.impersonate("62826bf03710200044e0bfc8bcbe5df1");
gs.info('FROM BACKGROUND SCRIPT2'+GlideImpersonate().isImpersonating());
impUser.impersonate(currentUser);
gs.info('FROM BACKGROUND SCRIPT3'+GlideImpersonate().isImpersonating());

 

 Thanks,
Rahul 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @rahulyamgar ,

 

check out this post 

 

If my answer helped you in any way, please mark it as helpful or correct. 

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Hi @rahulyamgar ,

 

check out this post 

 

If my answer helped you in any way, please mark it as helpful or correct. 

Ankur Bawiskar
Tera Patron
Tera Patron

@rahulyamgar 

did you check what came in logs and with which user?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

It looks to me that the way you scripted this, you are running into a session issue. There for you are just getting false as return.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

You are right- looks like an issue with session. When i added below line for impersonation, it just worked fine. I mean it returned true.

gs.getSession().impersonate("usernameORsysid")

 

Thanks Mark.