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

Brad Bowman
Kilo Patron
Kilo Patron

I don't think I'm understanding your situation completely.  Are you saying that the new approval record (that was cancelled) is correctly created, but there are other approvals that are not getting generated?  If so, are these other approvals the same or higher order?  Should they be created immediately when the UI action is clicked, or following the completion of the first approval record.

These are sequential approvals. Should create only when the first approval record is approved

Before or if you do not use the first UI action to cancel the approval, is there a flow that is running to handle the sequential approvals?  If so, is the flow running on the new approval record that is generated by the second UI action?

Yes, there is a flow created that is handling the sequential approvals. This is working fine. The issue with the generation of second approval by UI action. What changes to be done in the flow ? Also, I don't want to cancel the entire flow and restart it from the beginning.