approvals still active after Change was rejected.

gnani
Mega Expert

Hello Experts,

Please help me to resolve this.

Change was rejected and there are still existing approvals that were not cancelled. These are individual (manual) approvals that were added it didn't cancel them.

Thanks,

Nani.

1 ACCEPTED SOLUTION

This business rule is doing a whole lot more than your original question so I am unsure how to help with this.   What is the table on this business rule and what is the condition?



Basically you need to put a run script in your workflow that is connected to the Rejected condition on your approval.   This way if the approval is rejected it will run a script to cancel the manual approvals before the workflow ends. You can leverage some of the code in your business rule in the run script.   Though I see you are querying just where document_id matches the current.sys_id.   With approvals for task based tables, the sysapproval value on the sysapproval_approver table is also populated with the task sys_id.   You may want to change the query like I did below:


//reset all approvals


  var groupApprovalIDs= [];


  var currApprovals = new GlideRecord('sysapproval_approver');


  var qc = currApprovals.addQuery('document_id',current.sys_id);


  qc.addOrCondition('sysapproval',current.sys_id);


  currApprovals.query();



  while(currApprovals.next()){


  if(currApprovals.group != ''){


  groupApprovalIDs.push(currApprovals.group.sys_id.toString());


  }


  currApprovals.state = 'cancelled';


  currApprovals.comments = 'Change reset to draft.   Auto-cancelling all approval requests.';


  currApprovals.update();


  }



  //reset all group approval records found...


  var groupApprovals = new GlideRecord('sysapproval_group');


  groupApprovals.addQuery('sys_id', 'IN', groupApprovalIDs);


  groupApprovals.query();


  while(groupApprovals.next()){


  groupApprovals.approval = 'cancelled';


  groupApprovals.update();


  }


View solution in original post

8 REPLIES 8

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

The workflow will only cancel approvals that it "knows" about.   Since the approvals were created manually the workflow is unaware.   You can add a Run Script activity that will look for any pending approvals and cancel those.


Hi Michael,



I have written a business rule for this. Could you please look at it and help me where the run script should be added.



(function executeRule(current, previous /*null when async*/) {


  var Chtasks= new GlideRecord('change_task');


  Chtasks.addQuery('change_request',current.sys_id);


  //Chtasks.addQuery('u_type', 8); at this time we want all change tasks re-opened; even if they just close it right away. as it will ensure at least a re-check incase anything changed that they need to redo.. like re-analyze for anylsus type.


  Chtasks.query();


  while(Chtasks.next()){


  //moving re-accept notification code to BR on change task itself. So that no matter what, if acceptance changes back then we notify.


  Chtasks.state = '1'; //open


  Chtasks.u_task_acceptance='';


  Chtasks.work_start = '';


  Chtasks.work_end = '';


  //Chtasks.work_notes = 'The Change has been reset and this Change Task requires review.';


  Chtasks.update();


  }


  //reset workflow back to start as well


  current.work_start = '';


  current.work_end = '';


  current.approval = 'not requested';


  new Workflow().restartWorkflow(current, false);



  //reset all approvals


  var groupApprovalIDs= [];


  var currApprovals = new GlideRecord('sysapproval_approver');


  currApprovals.addQuery('document_id',current.sys_id);


  currApprovals.query();



  while(currApprovals.next()){


  if(currApprovals.group != ''){


  groupApprovalIDs.push(currApprovals.group.sys_id.toString());


  }


  currApprovals.state = 'cancelled';


  currApprovals.comments = 'Change reset to draft.   Auto-cancelling all approval requests.';


  currApprovals.update();


  }



  //reset all group approval records found...


  var groupApprovals = new GlideRecord('sysapproval_group');


  groupApprovals.addQuery('sys_id', 'IN', groupApprovalIDs);


  groupApprovals.query();


  while(groupApprovals.next()){


  groupApprovals.approval = 'cancelled';


  groupApprovals.update();


  }



})(current, previous);




Thanks,


Nani


This business rule is doing a whole lot more than your original question so I am unsure how to help with this.   What is the table on this business rule and what is the condition?



Basically you need to put a run script in your workflow that is connected to the Rejected condition on your approval.   This way if the approval is rejected it will run a script to cancel the manual approvals before the workflow ends. You can leverage some of the code in your business rule in the run script.   Though I see you are querying just where document_id matches the current.sys_id.   With approvals for task based tables, the sysapproval value on the sysapproval_approver table is also populated with the task sys_id.   You may want to change the query like I did below:


//reset all approvals


  var groupApprovalIDs= [];


  var currApprovals = new GlideRecord('sysapproval_approver');


  var qc = currApprovals.addQuery('document_id',current.sys_id);


  qc.addOrCondition('sysapproval',current.sys_id);


  currApprovals.query();



  while(currApprovals.next()){


  if(currApprovals.group != ''){


  groupApprovalIDs.push(currApprovals.group.sys_id.toString());


  }


  currApprovals.state = 'cancelled';


  currApprovals.comments = 'Change reset to draft.   Auto-cancelling all approval requests.';


  currApprovals.update();


  }



  //reset all group approval records found...


  var groupApprovals = new GlideRecord('sysapproval_group');


  groupApprovals.addQuery('sys_id', 'IN', groupApprovalIDs);


  groupApprovals.query();


  while(groupApprovals.next()){


  groupApprovals.approval = 'cancelled';


  groupApprovals.update();


  }


The Table is Change request[change_request] and the condition is "state changes to draft".