Suggestion for Impersonation in script

JaineelPetiwale
Tera Contributor

Hi All,

 

Could you help to suggest as a best practice, which function should I use to impersonate in script?

 

Option 1: As per Impersonate a user using a script - Support and Troubleshooting , But seems like this doesn't work in non-interactive session, but not sure.

gs.info(gs.getUserName());

session.onlineImpersonate("local_admin");

// Here you and do whatever you want to
gs.info(gs.getUserName());
// ....
// ....
// ....
// ....
// done

session.onlineUnimpersonate();

 

 Option 2: As per what community suggested, but it's not documented anywhere.

gs.info(gs.getUserName());

var systemUser = gs.getSession().impersonate("local_admin");

// Here you and do whatever you want to
gs.info(gs.getUserName());
// ....
// ....
// ....
// ....
// done

gs.getSession().impersonate(systemUser);

 

Option 3: As per what I got in SNC Docs and little bite help from community

var currentuser = gs.getUserName();

gs.info(gs.getUserName());

var impUser = new GlideImpersonate();

impUser.impersonate("local_admin");

// Here you and do whatever you want to
gs.info(gs.getUserName());
// ....
// ....
// ....
// ....
// done

impUser.impersonate(currentuser);

 

7 REPLIES 7

WillieW
Tera Contributor
session.onlineImpersonate("abel.tuter");
var usr = gs.getUserName();
gs.info('usr = ' + usr);
session.onlineUnimpersonate();

worked in my instance (run in Scripts - Background).

 

Who is 'local_admin'? A user_name value of a record in sys_user table?

Hemanth M1
Giga Sage
Giga Sage

Hi @JaineelPetiwale ,

 

May I know what your requirement is to impersonate in a script?

 

 

 

 

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Like we somethings write fix script and all, for bulk request like place order, or cancel tickets or anything else.

 

So, in that we want to do this action as either local_admin or system, so that admin who is running the script his name is not visible on record as the updated by or created by, instead either it should be local_admin or system, to so as generic.

 

Also, some times some actions are permitted by some specific user so in that case, if we are doing it with script we need to impersonate that specific user and then do it.

 

So, like this multiple use case are there, this is more of general question, not for any one specific case.

Ankur Bawiskar
Tera Patron
Tera Patron

@JaineelPetiwale 

unless you explain your requirement we can't help with the best approach.

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