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…
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:
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…
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:
<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>
Sounds complicated. What about the UI action having just the following script:
session.onlineImpersonate(current.sys_id);
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.
that was awesome
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?
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.
thx – copying didn’t work but typing did – great script!
If you just remove and readd the single quotes around ‘admin’, the issue goes away.
what is “sys_id=b071b5dc0a0a0a7900846d21db8e4db6” in the above script
That points to a specific UI page that ServiceNow uses to handle the impersonation.