- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 06:43 AM
I have an onBefore business rule that limits user query results based on which domain a user exists in. This rule interferes with trying to impersonate back to myself from a non-admin user (causes me to have to logout and log back in as myself).
Is there a condition I can write that will ignore this business rule if trying to impersonate?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 07:21 AM
The working solution I used was posted on here but must have been removed.
I added !gs.getImpersonatingUserName() to the condition line in my Business Rule script and am now able to successfully impersonate back to myself from a non-admin user.
Thanks all!
P.S. That gs.getSession().isImpersonating() looks like it will come in handy down the line too so thanks for that as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 08:52 PM
Hi Peter,
try this
var impersonatingUserName = gs.getImpersonatingUserName();
var impersonatingUserSysID = "";
var grUser = new GlideRecord("sys_user");
if(grUser.get("user_name", impersonatingUserName)) {
impersonatingUserSysID = grUser.getValue("sys_id");
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 07:21 AM
The working solution I used was posted on here but must have been removed.
I added !gs.getImpersonatingUserName() to the condition line in my Business Rule script and am now able to successfully impersonate back to myself from a non-admin user.
Thanks all!
P.S. That gs.getSession().isImpersonating() looks like it will come in handy down the line too so thanks for that as well.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 07:25 AM
Hi Nathan,
Thanks for sharing. FWIW, isImpersonating() returns a boolean. Your script is checking if there is not a (string) result returned. While both work, pragmatically, it makes more sense to check a boolean rather than checking for an empty/null string. From your description it sounded like you were more interested in knowing whether or not impersonation was being done than if the person being impersonated was empty (which will be the case 99.9999% of the time IMHO.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 07:43 AM
I agree on the boolean logic thing, but when I tried use !gs.getSession().isImpersonating(), it didn't work, and I got an error (this is the same error I was getting prior to using !gs.getImpersonatingUserName()😞
java.lang.NullPointerException
Check logs for error trace or enable glide.rest.debug property to verify REST request processing
java.lang.NullPointerException: java.lang.RuntimeException: java.lang.NullPointerException: com.glide.rest.handler.impl.ServiceHandlerImpl.handleInvocationTargetException(ServiceHandlerImpl.java:79)
com.glide.rest.handler.impl.ServiceHandlerImpl.invokeService(ServiceHandlerImpl.java:50)
com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:227)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:412)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:187)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:165)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:49)
com.glide.sys.ServletTransaction.run(ServletTransaction.java:34)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:748)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 07:47 AM
Thanks for the update. Looks like isImpersonating() may be broken. Good to know.