Business Rule condition ignore if trying to impersonate

nathan34
Giga Contributor

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?

1 ACCEPTED SOLUTION

nathan34
Giga Contributor

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.


View solution in original post

13 REPLIES 13

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");
}

reference: https://community.servicenow.com/community?id=community_question&sys_id=0c605729dbdcdbc01dcaf3231f96...

Regards
Ankur

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

nathan34
Giga Contributor

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.


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.)


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)


Thanks for the update. Looks like isImpersonating() may be broken. Good to know.