E

very 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’) && current.active == true && current.locked_out == false
Script:

function impersonateUser(){
   if(confirm('Do you want to impersonate this user?')){
      top.location.href = 'ui_page_process.do?sys_id=b071b5dc0a0a0a7900846d21db8e4db6&sys_user='+ 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
Description:
‘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:

<?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"/>

   <script>
      function impersonateUser(reference){
         var referenceField = reference.split('.')[1];
         if(confirm('Do you want to impersonate this user?')){
            top.location.href = 'ui_page_process.do?sys_id=b071b5dc0a0a0a7900846d21db8e4db6$[AMP]sys_user='+ g_form.getValue(referenceField);
         }
      }
   </script>
</j2:if>
</j:jelly>