Suggestion for Impersonation in script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @JaineelPetiwale ,
May I know what your requirement is to impersonate in a script?
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
unless you explain your requirement we can't help with the best approach.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader