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

We would need to see the flow.  After the first UI Action is this flow still executing?  If so, does it stay 'stuck' on a certain activity, if the second UI Action is used?  Does the same happen if the second UI Action isn't used, so the first UI Action is breaking the flow?

I checked the flow execution. After the first UI action (cancellation of approval), the flow gets stuck on the activity (which generates the approval). Used the second UI action which attached the same approval in Requested state (as expected). After approving it does not attach the next approval. The Flow remain stuck

I still need to see the flow(s) involved.  I'm thinking there should be a way to add one or more parallel activities that accommodate what the UI Actions are doing, maybe combined with changing them slightly.

I did some changes to the flow. Have put subflows (for approval) in "Do the following unti" flow logic now. it is working. thanks

AndersBGS
Tera Patron
Tera Patron

Hi @Karishma Mehta ,

 

why not handling your approval, cancel approval and retrigger approval through your flow instead of UI action?

 

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/