Impersonation testing

PHOTO EMBED

Fri Jun 12 2026 00:53:10 GMT+0000 (Coordinated Universal Time)

Saved by @kieoon #impersonation #testing

var testUserName = "userSysIdOrName"; // change
var me = gs.getUserID();

var session = gs.getSession();

try {
  // Only impersonate if testUserName is provided as an argument into the function)
  if (JSUtil.notNil(testUserName)) session.impersonate(testUserName);

  // what you want to run as the impersonated user - START
// e.g. GlideRecord query etc etc

  // what you want to run as the impersonated user - END
} catch (e) {
  gs.error(
    "Error whilst impersonating and running test:\n" + e,
  );
} finally {
  // Only impersonate back to the logged in user if testUserName is provided as an argument into the function)
  if (JSUtil.notNil(testUserName)) session.impersonate(me);
}
content_copyCOPY