Direct User Impersonation From a User Record

Every now and then I get a tip from a SNCGuru reader. This post comes courtesy of Garrett Griffin who emailed me yesterday with a cool script that his organization uses to allow admins to easily impersonate users without even having to select their name from the impersonate dialog. For those of you who don’t know about user impersonation in Service-now yet, you can read about it here. The method that Garrett shared is more convenient in many cases than the regular impersonate button and it also helps to eliminate the confusion that can be caused in the standard impersonate dialog when you’ve got more than one user with the same display name.

The scripts below use the exact same method as the regular impersonation dialog. The only difference is that they feed the user ID directly to the URL, which eliminates the need for you to choose a user to impersonate. This can be helpful when you’ve got a user record up and you want to impersonate from that user record, or when you are troubleshooting a bug in your Service-now instance that someone has reported in an incident. You can set up a UI action and/or a UI macro to easily impersonate the appropriate user in either of these scenarios.

Here’s how you could create a UI Action on the User form…

‘Impersonate’ UI Action
Name: Impersonate
Table: User (sys_user)
Action name: impersonateUser
Client: True
Form button: True
Onclick: impersonateUser()
Condition: gs.hasRole(‘admin’) && == true && current.locked_out == false

function impersonateUser(){
   if(confirm('Do you want to impersonate this user?')){
      top.location.href = ''+ g_form.getUniqueValue();

And here’s how you could modify Garrett’s solution above to create a UI Macro attached any user reference field in ServiceNow…

‘user_impersonate’ UI Macro
UI Macro icons require the creation of a UI Macro record under ‘System UI -> UI Macros’ (as shown below). They also require a dictionary personalization for the reference field the macro is associated with (as explained below).

Name: impersonate_user
‘Impersonate user’ link
Activate on any ‘sys_user’ reference field as follows:
– Set the active field in this macro to true
– Add the attribute to the reference field dictionary entry: ref_contributions=impersonate_user

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_guid" expression="gs.generateGUID(this)" />
<g:evaluate var="jvar_admin" expression="gs.hasRole('admin')" />
<j2:if test="${jvar_admin == true || jvar_personalizer == true}">
   <j:set var="jvar_n" value="impersonate_user_${jvar_guid}:${ref}"/>
   <g:reference_decoration id="${jvar_n}" field="${ref}"
    onclick="impersonateUser('${ref}'); "
    title="${gs.getMessage('Impersonate user')}" image="images/icons/roleKey_obj.gifx"/>

      function impersonateUser(reference){
         var referenceField = reference.split('.')[1];
         if(confirm('Do you want to impersonate this user?')){
            top.location.href = '$[AMP]sys_user='+ g_form.getValue(referenceField);

Date Posted:

December 10, 2010

Share This:


  1. Koen van Dooren December 10, 2010 at 1:59 am

    Sounds complicated. What about the UI action having just the following script:


    • Mark Stanger December 10, 2010 at 3:14 am

      Thanks for the comment. While that’s possible, it’s also server-side javascript so it does do the impersonation, but it doesn’t automatically refresh the browser so that you know you’re actually impersonating somebody. The other benefit to the client-side solution shown above is that it includes a confirmation prompt so that you don’t automatically impersonate a user by accidentally clicking a button.

  2. mathew May 4, 2011 at 3:13 am

    that was awesome

  3. Andreas August 8, 2011 at 11:04 am

    When saving the UI Action there is a JavaScript check taking part for the condition field which complains about the brackets for the role check function (“JavaScript parse error at line (1) column (12) problem = illegal character”).

    Is there a way to avoid this error without having the dictionary entry for that field changed?

    • Mark Stanger August 8, 2011 at 2:54 pm

      It’s probably just a copy/paste issue. I’ve re-added the code to the article above so you can try copying it again and see if that helps. If it doesn’t, then you can simply type the same information directly into the ‘Condition’ field manually to avoid the error.

      • Andreas August 8, 2011 at 3:21 pm

        thx – copying didn’t work but typing did – great script!

      • Paula Cullen December 20, 2011 at 8:32 am

        If you just remove and readd the single quotes around ‘admin’, the issue goes away.

  4. Dinesh November 7, 2016 at 4:09 am

    what is “sys_id=b071b5dc0a0a0a7900846d21db8e4db6” in the above script

    • Mark Stanger November 7, 2016 at 4:40 am

      That points to a specific UI page that ServiceNow uses to handle the impersonation.

Comments are closed.




Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.