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

Reset flow related to RITM

shaik23
Tera Expert

Hello mates,

 

i want to share this scenario which is working fine form me, if you have related query it would be helpful,

 

i have created a UI action to reset the flow related to RITM and approvals.

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 {
        // 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);
        }

        // 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);
        }

        // 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);
        }

        //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);

        //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);

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

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

 

check client and use (checkOpen()) in Onclick & put (reopen_ritm) in Action name.

 

If it helps mark as helpfull.

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@shaik23 

So you wanted to share your code which will be helpful for other members?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  yes just wanted to share code so it will helpful someone.