Update record as other user

Igor Kozlov
Tera Expert

Is it possible?

Or may be there is an other workaround for my case.

What i am trying to do:

some of   my RITMs could only be created automaticly. Let me call them child RITM.

I have a busines rule with logic: after a child RITM is created it find a parent RITM and copy watch list   and comments/work notes from it to child.

Here is a thing a don't like: all copied comments are created by user that triggered business rule.

I would like them to be created by some other user named "System".

Thanks

1 ACCEPTED SOLUTION

So no way to   do something like "impersonate while updating" ?



Updating of one Scheduled Script Execution is potential sourse of problem in cause of concurrent updates.



But thank for idea it lead me to ScheduleOnce that runs update as   System user.


So my current solution is to move all logic to script and cal it with ScheduleOnce.



var so = new global.ScheduleOnce();


so.script = "new SomeScript().someMethod('" + current.sys_id + "')";


so.schedule();



upd. Here is an example how to impersonate and save as any user:


var gr = new GlideRecord('sc_req_item')


gr.get('34069d870fc6ee003685ee68b1050ed9')



gr.comments = "test comments"


gr.work_notes = "test worknotes"




//now impersonate saving original user id


var origUser = gs.getSession().impersonate('161ce02e4f166640e11fe3414210c76b');


gs.print(origUser)



gr.update("impersonate testing")



//switch back to original user


var impersonated = gs.getSession().impersonate(origUser );


gs.print(impersonated)


View solution in original post

4 REPLIES 4

Geoffrey2
ServiceNow Employee
ServiceNow Employee

A Scheduled Script Execution has a Run as field for specifying a User.   You could try using your Business Rule to update a Scheduled Script Execution, then execute the script from the BR.


So no way to   do something like "impersonate while updating" ?



Updating of one Scheduled Script Execution is potential sourse of problem in cause of concurrent updates.



But thank for idea it lead me to ScheduleOnce that runs update as   System user.


So my current solution is to move all logic to script and cal it with ScheduleOnce.



var so = new global.ScheduleOnce();


so.script = "new SomeScript().someMethod('" + current.sys_id + "')";


so.schedule();



upd. Here is an example how to impersonate and save as any user:


var gr = new GlideRecord('sc_req_item')


gr.get('34069d870fc6ee003685ee68b1050ed9')



gr.comments = "test comments"


gr.work_notes = "test worknotes"




//now impersonate saving original user id


var origUser = gs.getSession().impersonate('161ce02e4f166640e11fe3414210c76b');


gs.print(origUser)



gr.update("impersonate testing")



//switch back to original user


var impersonated = gs.getSession().impersonate(origUser );


gs.print(impersonated)


"Run as" is exactly "impersonate while updating".



I haven't seen ScheduleOnce before.   That's a good one to know.


Hey Geoffrey, I have found a way to impersonate without "Rus as". Please take a look at my updated answer