restart workflow in req

rognomolta
Tera Contributor

Hi all,

We created a UI action button to restart the RITM flow and the workflow in REQ. When we clicked the restart button, the RITM flow restarted and the state and stage completed, but the workflow in REQ did not work correctly. The parent REQ should also be closed when the RITM was finished.
Here is the ui action script:

(function() {

    var req = new GlideRecord('sc_request');
    req.addQuery('sys_id', current.request);
    req.query();

    if (req.next()) {
        if (req.request_state == 'closed_incomplete') {
            req.active = true;
            req.request_state = 'requested';
            req.setWorkflow(false);
            req.update();
            new Workflow().restartWorkflow(req);

        }
    }
    var wfContext = new GlideRecord('wf_context');
    wfContext.addQuery('table', 'sc_request'); 
    wfContext.addQuery('id', req.sys_id); 
    wfContext.query();

    if (wfContext.next()) {
   
        var workflowId = wfContext.workflow.toString(); 
        var wflow = new global.Workflow();

        // Cancel the workflow context
        gs.info('Cancelling workflow context: ' + wfContext.sys_id);
        wflow.cancel(wfContext);

        // Start a new workflow for sc_request
        gs.info('Starting new workflow for sc_request: ' + req.sys_id);
        wflow.startFlow(workflowId, req, null); // Start the same workflow again on sc_request
    } else {
        gs.error('Error: Workflow context not found for sc_request: ' + req.sys_id);
    }

    var approvals = new GlideRecord('sysapproval_approver');
    approvals.addEncodedQuery('sysapproval=' + current.sys_id.toString());
    approvals.orderByDesc('sys_created_on');
    approvals.query();

    while (approvals.next()) {
        approvals.state = 'cancelled';
        approvals.update();
    }
    // Find and cancel related flow context records for 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(), null);
    }

    // Restart the Flow Designer flow for RITM
    try {
        // Restart the flow context for RITM
        sn_fd.FlowAPI.getRunner().restartFlowFromContext(grFlowContext.sys_id.toString(), null);

    } catch (ex) {
        gs.error('Error occurred: ' + ex.getMessage());
    }

    // Update the RITM record
    current.active = true;
    current.state = '1'; // Set to active
    current.update();

    // Add a work note to the RITM
    current.work_notes = 'Request has been restarted by ' + gs.getUserDisplayName();
    current.update();

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

})();

Please see the screenshot attached for your reference:

RITM state and stages 

rognomolta_0-1728628806873.png

REQ state and stages

rognomolta_1-1728628853538.png

REQ workflow

rognomolta_2-1728628888167.png

 

please guide me
Thank you,     

1 REPLY 1

Abbas_5
Tera Sage
Tera Sage

Hello @rognomolta,

Please refer to the link below:
https://www.servicenow.com/community/developer-forum/restart-workflow-for-one-req-or-ritm/m-p/149825...


If it is helpful, please mark it as helpful and accept the correct solution. In future, it might be helpful for someone to refer to this solution.

Thanks & Regards,

Abbas Shaik