
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2022 06:00 AM
Hi everyone,
I have a Business rule that runs onAfter insert/update sc_req_item records and I want to set a comment and the 'State' on the activity log with the system user, but I'm having duplicate entries (One with the logged user and one with the impersonated user).
I already have tried to use the setWorkflow(false), but the impersonate doesn't work.
gs.getSession().impersonate('admin');
current.comments = 'text';
current.setWorkflow(false);
current.update();
session.onlineUnimpersonate();
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2022 06:43 AM
Hello Thayane,
If you do the impersonate in a business rule, you will end up by being logged-off from your current session.
The best option for you is:
1) Create an event. Let's call it "system.record.comment";
2) Create a script action associated with that event with the following code:
execFun();
function execFun(){
var gr = new GlideRecord("incident");
gr.get(event.instance);
gs.getSession().impersonate('system');
gr.comments = "Text";
gr.update();
}
3) In the business rule you created before, replace your code by the following:
(function executeRule(current, previous /*null when async*/) {
gs.eventQueue("system.record.comment", current);
})(current, previous);
This way, after an update you will see this in the activity stream:
Note: due to the fact that this is being processed by events, you might get your comment in 1 second or 10 seconds after the record is saved. It will depend on the amount of events to be processed.
Hope this helps you achieve your desired output.
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Best Regards,
Filipe Cruz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2022 06:42 AM
Hi
Please run this business rule before the update and remove current.update() from the code.
Regards,
Gunjan
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2022 06:51 AM
Hi Gunjan,
Got curious with your suggestion and tried it out in my PDI.
The result was that the comment was saved but not by the system user but rather by the current user.
That makes sense: that on Before BR only added the comment but did not saved it. The save was performed after all Business rules are executed, and it was done by the current user.
The trick here is to do an execution that is not running on an interactive way. The event, being processed in the background, can achieve the desired behavior.
Best Regards,
Filipe Cruz

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2022 06:43 AM
Hello Thayane,
If you do the impersonate in a business rule, you will end up by being logged-off from your current session.
The best option for you is:
1) Create an event. Let's call it "system.record.comment";
2) Create a script action associated with that event with the following code:
execFun();
function execFun(){
var gr = new GlideRecord("incident");
gr.get(event.instance);
gs.getSession().impersonate('system');
gr.comments = "Text";
gr.update();
}
3) In the business rule you created before, replace your code by the following:
(function executeRule(current, previous /*null when async*/) {
gs.eventQueue("system.record.comment", current);
})(current, previous);
This way, after an update you will see this in the activity stream:
Note: due to the fact that this is being processed by events, you might get your comment in 1 second or 10 seconds after the record is saved. It will depend on the amount of events to be processed.
Hope this helps you achieve your desired output.
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Best Regards,
Filipe Cruz

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2022 07:33 AM
Hi
It worked! Thanks a lot, but I need the state log to get saved by the impersonated user too.