- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2022 07:02 PM
I have a requirement to restart a workflow, and go down a different branch to require different approvals than what was run the first time.
I have run into an issue where approvals that were required on the first run of the workflow (before it was restarted and went down a path where it no longer requires those approvals) are still being required in order to progress the change to Scheduled.
When I restart the workflow, I am using
new Workflow().restartWorkflow(gr, false);
So the approval status is not saved. But I need to set all of those approvals to cancelled so that they are no longer there, right? So I change their state to No Longer Required or Cancelled, but when the workflow restarts they are still required, and someone with approval_admin has to come in and change one of them to Approved because they are shown as No Longer Required/Cancelled and the average Joe can't update them because of ACLs.
This is the code I am using to cancel/no longer require approvals. Note that I have tried setting these approvals to cancel initially, and had the same result I have when setting them to no longer required.
var grApproval = new GlideRecord('sysapproval_approver');
grApproval.addQuery('sysapproval', grChg.sys_id);
grApproval.query();
while (grApproval.next()) {
if(grApproval.wf_activity.name.toString().toLowerCase().includes('risk approval') == true) {
grApproval.state = 'not_required'; Period';
grApproval.update();
} else {
grApproval.state = 'cancelled';Period';
grApproval.update();
}
}
I'm not sure if it makes a difference, but the approvals in question are created by a Change Approval Policy on the out-of-the-box Change Request - Normal workflow. I added an OR condition to the Change Approval Policy's Finish condition of State = New since I am setting the change back to New when I restart it, but no dice. Here is that OOB script:
var grApproval = new GlideRecord('sysapproval_approver'); // set all current approvals to Cancelled
grApproval.addQuery('sysapproval', grChg.sys_id);
grApproval.query();
while (grApproval.next()) {
if(grApproval.wf_activity.name.toString().toLowerCase().includes('risk approval') == true) {
grApproval.state = 'not_required';
grApproval.update();
} else {
grApproval.state = 'cancelled';
grApproval.update();
}
}
Everything else works completely fine. How can I fix this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2022 01:21 PM
Cancelling and restarting did not work for me (I had already tried that) but I ended up solving this by using the ChangeRequestStateHandler script include:
new Workflow().cancel(gr);
new Workflow().startFlow('workflow_sys_id', gr);
new ChangeRequestStateHandler(grChg).disassociateApprovalsFromWorkflow();
I was also able to base my code off of the disassociateApprovalsFromWorkflow() to remove specific approvals as well. All it does is set the approval state = 'cancelled' and wf_activity = ''. I was also able to use No Longer Requested instead of cancelled. Seems like the main thing is clearing the wf_activity field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2022 07:50 PM
Hi
did you also try to cancel the workflow and then start again?
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2022 01:21 PM
Cancelling and restarting did not work for me (I had already tried that) but I ended up solving this by using the ChangeRequestStateHandler script include:
new Workflow().cancel(gr);
new Workflow().startFlow('workflow_sys_id', gr);
new ChangeRequestStateHandler(grChg).disassociateApprovalsFromWorkflow();
I was also able to base my code off of the disassociateApprovalsFromWorkflow() to remove specific approvals as well. All it does is set the approval state = 'cancelled' and wf_activity = ''. I was also able to use No Longer Requested instead of cancelled. Seems like the main thing is clearing the wf_activity field.