Requested state stuck in pending approvals

shaik17
Tera Contributor

Hello all,

i had a scenario when the approvals in RITM rejected or the RITM cancelled the state on RITM should set to open and REQ requested state should be in pending approval, i have created a UI action 'Restart' when clicking the UI action the existing approvals should cancel and have to re trigger a new flow designer i have written a script, i am able to retrigger new flow and approvals also getting triggered but when approving the new approvals and after closing the sc task the RITM went to close complete but the REQ stuck in pending apprvals. I have shared the script below. Please assist further.

shaik17_0-1728884879118.png

 

function checkOpen() {
    var userConfirmation = confirm('Do you want to Reopen this RITM again?');
    if (userConfirmation) {
        gsftSubmit(null, g_form.getFormElement(), 'reopen_ritm');
    }
}

if (typeof window == 'undefined') {
    restartFlow();
}

function restartFlow() {
    try {
        // Step 1: Reopen and update the parent request (REQ)
        var reqRecord = new GlideRecord('sc_request');
        if (reqRecord.get(current.request)) {
            reqRecord.setWorkflow(false); // Prevent workflow triggers on update
            reqRecord.request_state = 'requested'; // Set REQ to 'requested'
            reqRecord.active = true;
            reqRecord.update();
            gs.info('Parent REQ reopened: ' + reqRecord.number);

            // Restart traditional workflow for REQ
            var wflow = new Workflow();
            wflow.restartWorkflow(reqRecord);
            wflow.runFlows(reqRecord, "update");
        } else {
            throw new Error('Unable to find parent request (REQ) for RITM: ' + current.number);
        }

        // Step 2: Cancel all related approvals
        var approvals = new GlideRecord('sysapproval_approver');
        approvals.addQuery('sysapproval', current.sys_id.toString());
        approvals.orderByDesc('sys_created_on');
        approvals.query();

        while (approvals.next()) {
            approvals.state = 'cancelled';
            approvals.update();
            gs.info('Approval cancelled: ' + approvals.sys_id);
        }

        // Step 3: Cancel existing flow contexts for the RITM
        var grFlowContext = new GlideRecord('sys_flow_context');
        grFlowContext.addQuery('source_record', current.sys_id.toString());
        grFlowContext.orderByDesc('sys_created_on');
        grFlowContext.query();

        while (grFlowContext.next()) {
            sn_fd.FlowAPI.cancel(grFlowContext.sys_id.toString(), 'cancel');
            gs.info('Flow context cancelled: ' + grFlowContext.sys_id);
        }

        // Step 4: Restart the Flow Designer flow for the RITM
        var flow = current.cat_item.flow_designer_flow;
        if (!flow) throw new Error('Flow Designer flow not found for RITM: ' + current.number);

        var flowName = flow.sys_scope.scope + "." + flow.internal_name;
        var inputs = { 'table_name': 'sc_req_item', 'request_item': current };
        var contextId = sn_fd.FlowAPI.startFlow(flowName, inputs);

        // Step 5: Update RITM state and fields
        current.state = '1'; // Set to 'Open'
        current.active = true;
        current.setValue('flow_context', contextId);
        current.work_notes = 'Request restarted by ' + gs.getUserDisplayName();
        current.update();
        gs.info('RITM restarted: ' + current.number + ' with new Flow Context: ' + contextId);

        // Step 6: Redirect to the RITM record
        action.setRedirectURL(current);

    } catch (ex) {
        gs.error('Error in restartFlow: ' + ex.message);
    }
}

 


REQ:

shaik17_1-1728884942781.png

shaik17_2-1728885046442.png

shaik17_3-1728885097121.png

 

 

0 REPLIES 0