Suggestion for Impersonation in script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2025 05:41 AM
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
09-11-2025 09:46 PM
For auditing and reporting purposes, execution of a script via impersonation would complicate tracking.
Glide Impersonate is used to replicate scenarios to check whether users can execute something and is used for testing & troubleshooting purposes.
Depending on your requirements, you can opt for the function that suits you. But make sure you are not using it as part of scheduled jobs or scripts execution.
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2025 06:29 AM - edited 09-13-2025 07:28 AM
// show current user
var usr = gs.getUserName();
gs.info('usr = ' + usr);
// impersonate
session.onlineImpersonate("abel.tuter");
var usr = gs.getUserName();
gs.info('usr = ' + usr);
// end impersonation
session.onlineUnimpersonate();
// show current user
var usr = gs.getUserName();
gs.info('usr = ' + usr);worked in my instance (run in Scripts - Background).
And us a valid 'user_name' value from sys_user table. "local_admin" doesn't exist in my instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2025 04:26 PM
may be helpful.
