Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to cancel aks for approval when record is updated.

Chang Shuang
Tera Contributor

Hi All,

I am creating a flow designer, and I want to set the ask for approval cancelled if the record is updated .

How can I set the flow Designer.

Thank you very much.

1 ACCEPTED SOLUTION

S Goutham
Tera Guru

Hey @Chang Shuang  :
May I know why you want to cancel the approval after an update over the record, and what on what update do you want to perform this action like a state change of the parent record? 

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue

View solution in original post

5 REPLIES 5

S Goutham
Tera Guru

Hey @Chang Shuang  :
May I know why you want to cancel the approval after an update over the record, and what on what update do you want to perform this action like a state change of the parent record? 

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue

HI Goutham,

I want to cancel the approval request, after update one of fields of the table.

 

I would suggest writing a Business rule on the table where you going to update the record 
Set the BR condition on the <field> changes as the trigger  

Set it to After update

Set Advanced to <True>

use the below script to close the approval record 

    var grApprovalRecord = new GlideRecord("sysapproval_approver");
    grApprovalRecord.addQuery("document_id", <your record>.sys_id);
    grApprovalRecord.query();
    while (grApprovalRecord.next()) {
      if (grApprovalRecord.state == "requested") {
        new Workflow().cancel(grApprovalRecord); // Cancel approval workflow
        grApprovalRecord.setWorkflow(false); // Prevents sending of emails
        grApprovalRecord.state = "not_required";
        grApprovalRecord.update();
      }
    }
    
    // Group approval canceller
    // Locate every Group approval and set to - No Longer Required
    var grGroupApprovalRecord = new GlideRecord("sysapproval_group");
    grGroupApprovalRecord.addQuery("parent", <your record>.sys_id);
    grGroupApprovalRecord.query();
    while (grGroupApprovalRecord.next()) {
      if (grGroupApprovalRecord.approval == "requested") {
        new Workflow().cancel(grGroupApprovalRecord); // Cancel group approval workflow
        grGroupApprovalRecord.setWorkflow(false); // Prevents sending of emails
        grGroupApprovalRecord.approval = "not_required";
        grGroupApprovalRecord.update();
      }
    }

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue

hi Goutham,

 

Thanks for your repay.

I add this gs.info code before the code you shared with me.

I found that it will give several log messages at the same time when I update the record.

gs.info(previous.u_security_incident_state);
ChangShuang_0-1693833928359.png