After Business Rule with Impersonate code

Thayane
Tera Contributor

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();

 

find_real_file.png

1 ACCEPTED SOLUTION

Filipe Cruz
Kilo Sage
Kilo Sage

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:

find_real_file.png

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

 

View solution in original post

6 REPLIES 6

Maik Skoddow
Tera Patron
Tera Patron

Hi @Thayane 

for me, it's not really clear what your issue is.

Your screenshot is about changing the "State" but in your code you just add a comment.

Kind regards
Maik

Hi @Maik Skoddow 

You're right. I posted the screenshot and forgot to write the second part of the question. I was having problems with the comment, but also getting the State log duplicated.