How do I end Impersonation in a script in the workflow

Stacy1
Mega Guru

I am creating new accounts via workflow.  Only Admin can create accounts per business rules to create the account code and path.

So in my workflow I am impersonating admin before creating the account.  

However I need to end the impersonation in the script because it logs the user off.

//Only admin can create accounts, must impersonate admin
var impUser = new GlideImpersonate();
impUser.impersonate('6816f79cc0a8016401c5a33be04be441');
//Create Account

How do I stop impersonating?

Thanks,

Stacy

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

If I am following you correctly then you have impersonated an admin user in the workflow script and now you want to impersonate back to logged in user? I believe you need to re impersonate the logged in user. Try something like this 

var currentUser = gs.getUserID();
var impUser = new GlideImpersonate();
impUser.impersonate('6816f79cc0a8016401c5a33be04be441');
impUser.impersonate(currentUser);

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

View solution in original post

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

The workflow runs as a system user, so you shouldn't ever need to impersonate an admin.  Did you try this and get an error or unexpected results without impersonation?

The next step in a workflow runs as the person that triggered it.  

So I have an approval and it triggers the script which the system thinks is that person that approved.  I tested it by adding the admin role to the approver and it did not have a problem adding the account.  When that user does not have the admin role it will not create the account and I get an error.  The error is based on a business rule on creating account paths for new accounts which require the admin role.

 

Hitoshi Ozawa
Giga Sage
Giga Sage

Following in the documentation. This implicitly also implies that as Brad stated above, you shouldn't be needing to impersonate an admin.

Ensure that once you are finished impersonating a user that you call the method again with the administrator sys_Id to stop the impersonation.

As written in the same documentation page, impersonating is only for testing purposes and shouldn't be used otherwise.

The global GlideImpersonate API enables administrators to pose as another authenticated user for testing purposes.

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/GlideImpersonateAPI

My bad on this. Skipped reading that this question is about workflow. Workflow can run as specified user.

Have you tried saving the userSysId of the user started the flow and then calling impersonate() with that userSysId?