Change peer reviewer

Brendan Hallida
Kilo Guru

Hi all,

We have a fairly standard change workflow, which when it gets to the peer review stage, sends an approval to the user that is added to the u_peer_reviewer field.   The peer reviewer then approves or denies the change.

An issue has come up where the person peer reviewing may need to change (this happens more than you would think), however if this stage has already been reached, the peer review is still with the original peer reviewer.

Here is an example of what I mean

1st Peer Reviewer

find_real_file.png

Approvers tab

find_real_file.png

We have changed our peer reviewer because of reasons

find_real_file.png

Approvers tab - as you can see, it still has the first peer reviewer.

find_real_file.png

What is the best way to address this?   Essentially we would like it if the peer reviewer changes field changes, cancel the existing peer reviewer approval and create a new one.

here is our change workflow

find_real_file.png

I hope this explains everything, and any advise would be most appreciated

Cheers,

Brendan

1 ACCEPTED SOLUTION

Ok, I think I got it!



First of all, I hope someone much smarter than me has a more elegant solution. Actually, I don't think this solution is TOO terrible. Here goes.



Everything you have seems right except we need to tell the Approval activity (the one that is running through as Approved even when you roll back) not to consider "Skipped" as "Approved. To do that we have to edit the Workflow Condition. Locate your Approval workflow activity and click on the "Approved" condition.


find_real_file.png



Then, edit the condition line to say: activity.result == 'approved'



Finally, after the rollback, you have to set your State to something like Work in Progress. I find that the rollback and change in approvers is forcing my task state to be Requested, even if that's not a valid state.



Give it a whirl and you should be good to go! I tested the above steps in my instance and got this to work so you should have success with this method.



My instance:



First, the approver is Aada Larsen. I derived this from a field I created on my form called "Approver":


find_real_file.png



Then I change the approver to "Abel Tuter" on my form. I then manually executed the rollback via Background script:


find_real_file.png



Anf finally, in my task record, the approver is Abel Tuter:


find_real_file.png




I hope this helps! If so, mark the answer is helpful and set this post to Answered.



Good luck, Brendan!


View solution in original post

10 REPLIES 10

jcraneNYC
ServiceNow Employee
ServiceNow Employee

You may be able to accomplish this with a Wait for WF Event acivity.



Off of your "Set to Peer Review" activity, drag a second connector out to a Wait for WF Event activity. In the Wait For Event field of your new activity, type in "peer_reviewer_changed". The Wait For will connect to a Rollback activity that will then circle around back to your "Set to Peer Review", forming a loop between Set to Peer Review, Wait for WF Event, and Rollback.



Now, create a Business Rule on your Change record that executes After Update when Peer Reviewer changes and Peer Reviewer is not empty. The script in this BR would be:



var w = new Workflow();


w.fireEvent(current, 'peer_reviewer_changed');



This will fire the "peer_reviewer_changed" event into the workflow, which will trigger the rollback, canceling the current approvals and re-requesting the new Peer Reviewer.


Hi Jon,



Thanks for this.



I have it implemented, however I am getting the following error in the logs



org.mozilla.javascript.EcmaError: "peer_reviewer_changed" is not defined.


    Caused by error in sys_ui_action.47fd7f4dc0a8000600a552278b5232ab.script at line 2




          1: var w = new Workflow();


==>     2: w.fireEvent(current, 'peer_reviewer_changed');



Do you know why this is?



Cheers,


Brendan


Ignore the other post, it was my gs.log statement that i threw in to see if the BR was working.   Fixed that and it is.



In saying that - the workflow is not rolling back.   Where can I see the workflow event that we fire in the BR?   The workflow for the change is stuck on the Wait for WF Event.


Oops, I picked the wrong workflow script method.



fireEvent() either needs to happen within INSIDE a workflow, or it must accept a sys_id of an executing activity (from the wf_executing) table. This would be harder to programmatically derive but still doable!



What you need is broadcastEvent(). This broadcasts an event to a workflow by context ID. So your business rule is actually going to look like:



/* Pulled from the wiki */


var wf = new Workflow().getRunningFlows(current);


while(wf.next()) {    


    new Workflow().broadcastEvent(wf.sys_id, 'peer_reviewer_changed');


}



getRunningFlows() returns a listing of Workflow Context glide records for all contexts running in the task record. We then loop through the contexts and broadcast our event to the contexts. You will see your workflow update once it catches this event after you broadcast it.



You could get fancy and only broadcast the event to the required workflow by qualifying your loop further with an If statement inside your loop:


var wf = new Workflow().getRunningFlows(current);


while(wf.next()) {    


    if (wf.workflow_version.getDisplayValue() == 'My Cool Change Workflow')


                  new Workflow().broadcastEvent(wf.sys_id, 'peer_reviewer_changed');


}