Disassociate approvals from workflow is not working through UI action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
We have a custom UI action "Revert to Assess" in Authorize state of a change request. We have got a requirement to make it visible in Scheduled state as well. We are able to go back to Assess state but approvals are not disassociated. The approval records are still visible in the change request. The below highlighted one is not working and even tried wf_activity as empty. It would be appreciated if someone assist me on the same.
Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4m ago
The key part here is the order of execution. I would clean up and disassociate the approvals first, then move the change to Assess and save it, and only after that cancel or restart the workflow. That avoids the new workflow starting while the change is still in Scheduled.
Also use state on sysapproval_approver and approval on sysapproval_group, and consider setWorkflow(false) when updating those approval records so business rules do not fire unexpectedly.
I changed your script with the help of AI:
var chgState = current.getValue('state'); if (chgState == '-3') { // Authorize → Assess: add a comment only; no workflow manipulation is required current.comments = 'Change has been reverted from Authorize back to Assess'; } else if (chgState == '-2') { // Scheduled → Assess // 1. Disconnect approvals from the workflow engine new global.ChangeRequestStateHandler(current).disassociateApprovalsFromWorkflow(); // 2. Set user approvals to "Not Required" var approverGr = new GlideRecord('sysapproval_approver'); approverGr.addQuery('sysapproval', current.getUniqueValue()); approverGr.addQuery('state', '!=', 'cancelled'); approverGr.query(); while (approverGr.next()) { approverGr.setValue('state', 'not_required'); approverGr.setValue('wf_activity', ''); approverGr.setWorkflow(false); approverGr.update(); } // 3. Set group approvals to "Not Required" var groupApprovalGr = new GlideRecord('sysapproval_group'); groupApprovalGr.addQuery('parent', current.getUniqueValue()); groupApprovalGr.addQuery('approval', '!=', 'cancelled'); groupApprovalGr.query(); while (groupApprovalGr.next()) { groupApprovalGr.setValue('approval', 'not_required'); groupApprovalGr.setValue('wf_activity', ''); groupApprovalGr.setWorkflow(false); groupApprovalGr.update(); } // 4. Reset change-level fields current.setValue('approval', 'not requested'); current.setValue('u_cab_ready', false); current.comments = 'Change has been reverted from Scheduled back to Assess'; } // 5. Set the state to Assess and save the change current.setValue('state', '-4'); current.update(); // 6. Restart the workflow only after the change has been saved with state = -4 if (chgState == '-2') { var wf = new global.Workflow(); wf.cancel(current); wf.restartWorkflow(current); } action.setRedirectURL(current);
