Running a Business Rule as a specific user

jeremielanders
Mega Contributor

Greetings!

I have been trying to figure out a way to run a Business Rule in it's entirety as a user other than myself, or the user who has triggered the BR. Here is an example:

When work notes and customer notes are updated on an Outage, I have a BR that pushes those notes updates to the incidents associated with the outage. The notes in the associated incidents naturally show as me who added them. However, what we would like to do is show that a specific user (in this case a user named "System Update") actually added them instead.

Does anyone have a solution on how to do what I'm detailing? Any help is very much appreciated. Thanks!

Jeremie

1 ACCEPTED SOLUTION

StephenHey
Mega Guru

Depending on where your request is originating, you can do this using an 'async' business rule.   Being asynchronous, it temporarily pushes the script in the rule to the background.   When it brings it back to the foreground, it's running as user 'system'.   'System' has the innate ability to impersonate.   So, combining scripted impersonation with the 'system' account, you can write an async rule like this:



var journalGR = new GlideRecord('sys_journal_field');


journalGR.addQuery('element_id', current.sys_id);


journalGR.orderBy('sys_created_on');


journalGR.query();



while (journalGR.next()) {


        var convertedTask = new GlideRecord('task');


        convertedTask.get(current.u_converted_to);



        var userGR = new GlideRecord('sys_user');


        userGR.get('user_name', journalGR.sys_created_by);


        var originalUser = gs.getSession().impersonate(userGR.sys_id);



        convertedTask[journalGR.element] = journalGR.value.toString();


        convertedTask.update();



        gs.getSession().impersonate(originalUser);


}





Our use case was to convert 'tickets' to other task types like incident and FMRs.   This includes some extra code that you probably don't need that collects the journal entries of the ticket and puts them in the new task as the user who originally created them.


View solution in original post

12 REPLIES 12

StephenHey
Mega Guru

Depending on where your request is originating, you can do this using an 'async' business rule.   Being asynchronous, it temporarily pushes the script in the rule to the background.   When it brings it back to the foreground, it's running as user 'system'.   'System' has the innate ability to impersonate.   So, combining scripted impersonation with the 'system' account, you can write an async rule like this:



var journalGR = new GlideRecord('sys_journal_field');


journalGR.addQuery('element_id', current.sys_id);


journalGR.orderBy('sys_created_on');


journalGR.query();



while (journalGR.next()) {


        var convertedTask = new GlideRecord('task');


        convertedTask.get(current.u_converted_to);



        var userGR = new GlideRecord('sys_user');


        userGR.get('user_name', journalGR.sys_created_by);


        var originalUser = gs.getSession().impersonate(userGR.sys_id);



        convertedTask[journalGR.element] = journalGR.value.toString();


        convertedTask.update();



        gs.getSession().impersonate(originalUser);


}





Our use case was to convert 'tickets' to other task types like incident and FMRs.   This includes some extra code that you probably don't need that collects the journal entries of the ticket and puts them in the new task as the user who originally created them.


Stephen, this is fantastic. Thank you very kindly for the really useful solution. It's much appreciated!


abhishek_s
Tera Contributor

This is a very old question but SN's setJournalEntry method lets you attribute the work_notes on a task record to some other user. No impersonating or fancy scripting required -

 

if(now_GR.next()){
  now_GR.work_notes.setJournalEntry("Content of the journal entry.", "abel.tuter");  
  now_GR.update();
}