cancel all awaiting aprovals

Richard P
Mega Guru

It seems if a change is canceled that approvers still have an outstanding task to approve a job which approval is now no longer required.

I thought it would be a buisness rule, but that didnt seem to work.

can someone advise the best way that if a a change is canceled, we then change the state of all approvals to no longer required anc close all tasks related.

thanks

1 ACCEPTED SOLUTION

Hi,



I created a UI Action on the request item table to find and cancel outstanding approvals which is a pretty similar thing to what you're doing.



I used an if(approval.next) cos the existing BRs take into account the other approvals and mark them as not required....   It does highlight how to update the approval record though.... looks like you're referring to client script functions setValue(X,Y) etc etc



  approval.state = 'rejected';


  approval.comments = "Cancelled by " + user;


  approval.update();




Example Script


var user = gs.getUserDisplayName();



var approval = new GlideRecord('sysapproval_approver');  


      approval.addQuery('sysapproval', current.sys_id);


      approval.addQuery('state', 'requested');  


      approval.query();  



if(approval.next()){



  approval.state = 'rejected';


  approval.comments = "Cancelled by " + user;


  approval.update();


  gs.addInfoMessage("Request Item: " + current.number + " has been cancelled");




} else {


  gs.addInfoMessage("Request Item: " + current.number + " cannot be cancelled as there are no active approvals");


}//end else


View solution in original post

10 REPLIES 10

Hi,



I created a UI Action on the request item table to find and cancel outstanding approvals which is a pretty similar thing to what you're doing.



I used an if(approval.next) cos the existing BRs take into account the other approvals and mark them as not required....   It does highlight how to update the approval record though.... looks like you're referring to client script functions setValue(X,Y) etc etc



  approval.state = 'rejected';


  approval.comments = "Cancelled by " + user;


  approval.update();




Example Script


var user = gs.getUserDisplayName();



var approval = new GlideRecord('sysapproval_approver');  


      approval.addQuery('sysapproval', current.sys_id);


      approval.addQuery('state', 'requested');  


      approval.query();  



if(approval.next()){



  approval.state = 'rejected';


  approval.comments = "Cancelled by " + user;


  approval.update();


  gs.addInfoMessage("Request Item: " + current.number + " has been cancelled");




} else {


  gs.addInfoMessage("Request Item: " + current.number + " cannot be cancelled as there are no active approvals");


}//end else


Hi Ben,



thanks, I did see some results from your script when applied to my business rule, however it did not iterate through each of the outstanding approvals, only changing the first one, however if I changed your if to while, it seemed to do the trick. TYVM



heres my slight modification



var user = gs.getUserDisplayName();



var approval = new GlideRecord('sysapproval_approver');


      approval.addQuery('sysapproval', current.sys_id);


      approval.addQuery('state', 'requested');


      approval.query();



while(approval.next()){



  approval.state = 'No Longer Required';


  approval.comments = "Cancelled by " + user;


  approval.update();


  gs.addInfoMessage("Change: " + current.number + " has been cancelled");



}


Hi Richard,



New to java script as well and i am facing the same challenge. Can you please let me know the conditions to run in BR and the java scripts that you use that make it work?



Thanks,


Wai S


Richard,



Thank you for the script. I made slight modifications on your script as following in BR, use condition when "State" changes to Canceled and it works!



var user = gs.getUserDisplayName();



var approval = new GlideRecord('sysapproval_approver');


      approval.addQuery('sysapproval', current.sys_id);


      approval.addQuery('state', 'requested');


      approval.query();



while(approval.next()){



  approval.state = 'No Longer Required';


  approval.comments = "Cancelled by " + user;


  approval.update();


  }


gs.addInfoMessage("Change: " + current.number + " has been cancelled");


Ashutosh Munot1
Kilo Patron
Kilo Patron

So you want to say that once change is cancelled you want to make your approval state to cancelled