Is there a way to Start and End impersonation using a script ?

Bhavesh Jain1
Giga Guru

Is there a way we can impersonate with a user using a script...make updates with Dummy user...then end impersonation ?

4 REPLIES 4

Anurag Tripathi
Mega Patron
Mega Patron

Hi Bhavesh,



You can use the following line to impersonate another user through script



gs.getSession().impersonate(<sys id of user to impersonate>);






you can test this in a   background script by running the following




gs.getSession().impersonate(<sys id of user to impersonate>);


gs.print(gs.getUserName());


-Anurag

Thanks Anurag.


I have already tried this but it is causing issue in the workflow since we are only starting and not ending the session. I remember I wrote a script in past where I started and ended the impersonation through script but not getting that anywhere...it seems I lost that code from my repo.


mathankumar2
Kilo Contributor

Hi Bhavesh,



We have a requirement to validate the CI relationship type during the transform map import.



Ex:If mistakenly user enters a relationship type on the data load sheet which   is not defined between 2 CI's on the system then the import should not happen.But what I see is even though if relation ship is not defined between 2 CI's on the system   , say Network and Linux, if you still have a relation ship mentioned on the sheet called - Network123 Contains ::Contained by Linux123 ,, then the new relationship type between two CI's gets inserted in to CMDB, which should not happen as per the requirement.



I tried validating using the on before script by getting the CI class for parent,child and relationship type on the 'cmdb_rel_type_suggest' but no luck. Please suggest.


==================================================================


Also, please suggest if there is a way to ignore the complete sheet from being updated in to CMDB if a single record on the data load sheet fails the validation , as i know that we can ignore the particular row which failed during the validation.


=================================================================



Thanks,


Mathan R


chrishenson
ServiceNow Employee
ServiceNow Employee

Hello Bhavesh,



Impersonate returns the sys_id of the user performing the impersonation which can be stored and reverted to by calling impersonate again.   Example below impersonates and then reverts.



1
2
3
4
5
gs.print(gs.getUserName() + ":" + gs.getUserID());
var myUser = gs.getSession().impersonate("62826bf03710200044e0bfc8bcbe5df1");
gs.print(gs.getUserName() + ":" + gs.getUserID());
gs.getSession().impersonate(myUser);
gs.print(gs.getUserName() + ":" + gs.getUserID());


Regards,



Chris Henson