The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Cancel Approval and then Retrigger Approval on RITM

Karishma Mehta
Tera Contributor

I am working on a POC on my PDI where I have to create two UI actions: 

1. Cancels the Approval Record

2. Retriggers the approval record.

 

I have created a Catalog item, Flow, two UI action buttons.

The UI action for cancelling the approval record is working fine. However, the UI action for retrigger approval is just adding the approval which is cancelled and not adding the next approval record.

Do I have to make changes to the UI action script or Flow for this?

Following scripts are for both the UI actions respectively:

1. For cancelling the approval record:

function backToNew() {
    var answer = confirm("Are you sure you want to cancel the approval record?");
    if (answer) {
        gsftSubmit(null, g_form.getFormElement(), 'back_to_new');
    }
}


if (typeof window == 'undefined') {
    // Cancel the workflow
    // new Workflow().cancel(current);
    gs.log("Server-side logic triggered for RITM: " + current.number);

    // Cancel active approvals manually
    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'requested');
    gr.query();
    while (gr.next()) {
        gr.state = 'cancelled';
        gr.update();
    }
    current.state = '-5';
    current.work_notes = "Approval has been cancelled and RITM moved to Pending";
    current.update();

    // Redirect to the updated RITM
    action.setRedirectURL(current);

}
 
2. For Retrigger Approval
function requestForApproval() {
    var answer = confirm("Do you want to re-trigger approval?");
    if (answer) {
        gsftSubmit(null, g_form.getFormElement(), 'request_for_approval');
    }
}

if (typeof window == 'undefined') {

    // Get all cancelled approvals for this RITM, ordered by sequence
    var approvalGR = new GlideRecord('sysapproval_approver');
    approvalGR.addQuery('sysapproval', current.sys_id);
    approvalGR.addQuery('state', 'cancelled');
    approvalGR.orderBy('order'); // Ensure sequential order
    approvalGR.query();

    var count = 0;

    while (approvalGR.next()) {
        var approverID = approvalGR.approver.toString();
       
        // Check if this approver already has a requested or approved record
        var existing = new GlideRecord('sysapproval_approver');
        existing.addQuery('sysapproval', current.sys_id);
        existing.addQuery('approver', approverID);
        existing.addQuery('state', 'IN', 'requested,approved,cancelled');
        existing.addQuery('sys_id', '!=', approvalGR.sys_id);
        existing.query();

        if (existing.hasNext()) {
            continue;
        }

        // Check if all previous approvals are approved
        var priorGR = new GlideRecord('sysapproval_approver');
        priorGR.addQuery('sysapproval', current.sys_id);
        priorGR.addQuery('order', '<', approvalGR.order);
        priorGR.addQuery('state', 'NOT IN', 'approved,cancelled');
        priorGR.query();

        if (priorGR.hasNext()) {
            continue;
        }

        // re-trigger this approval
        var newApproval = new GlideRecord('sysapproval_approver');
        newApproval.initialize();
        newApproval.approver = approverID;
        newApproval.sysapproval = current.sys_id;
        newApproval.state = 'requested';
        newApproval.order = approvalGR.order;
        newApproval.insert();
        count++;
    }

    current.work_notes = count > 0 ?
        count + ' approval(s) re-triggered successfully.' :
        'No eligible approvals found to re-trigger.';
    current.update();

    gs.addInfoMessage(current.work_notes);
    action.setRedirectURL(current);
}



12 REPLIES 12

Business wants to have an option to cancel and re trigger the approvals.

HI @Karishma Mehta ,

 

i do understand that. Based on that statement, i would suggest that you have a UI action to cancel/reject approval, but control the re trigger within the flow.

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Business wants to have control over both. Cancellation of approval and retrigger as well. The UI action for retrigger of approval is attaching the first approval record but failing to attach the next ones.