restart flow designer

shaik23
Tera Expert

Hello Experts,

i have below scenario:

Create UI action to restart cancelled/rejected Requested Item

Name: Restart

Available when state = Closed Skipped or Closed Incomplete

Requires role Catalog Admin

  1. If Parent REQ is closed, mark as active and set state to Pending Approval. Restart workflow

  2. Set RITM state to 1 and active to true

  3. Restart (Or re-run) flow associated with the RITM

  4. Add work note: Request has been restarted by (current user display name)

i have written a script the script changes the states as per description and cancelling the approvals but not restarting the flow designer associated to RITM.
Please assist me.

 

 

//script to cancell the current flow designer and srart new flow


(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.request_state == 'closed_skipped') {
            req.setWorkflow(false);
            req.active = true;
            req.request_state = 'requested';
            req.update();

            // Restart workflow for REQ
            new Workflow().restartWorkflow(req);
        }
    }

    // Update approval records for RITM
    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(), 'cancel');
    }
    
    // Restart the Flow Designer flow for RITM with new inputs
    try {
        var inputs = {
            'request_item': current.sys_id.toString(),
            'table_name': 'sc_req_item'
        };

        var contextId = grFlowContext.sys_id.toString();

        var runner = sn_fd.FlowAPI.getRunner();
        if (runner) {
          
			// we also tried below API's but not working
            runner.FlowAPI.startFlow('global.	', inputs);
            //sn_fd.FlowAPI.executeFlow('global.hardware_requests', inputs);
        } else {
            gs.error('Flow Runner not found.');
        }

        // Optionally update the flow context record with new details
        var updatedFlowContext = new GlideRecord('sys_flow_context');
        if (updatedFlowContext.get(contextId)) {
            updatedFlowContext.source_table = 'sc_req_item';
            updatedFlowContext.source_record = current.sys_id.toString();
            updatedFlowContext.update();
        }
    } catch (ex) {
        gs.error('Error occurred: ' + ex.getMessage());
    }
    
    // Update the RITM record
    current.active = true;
    current.approval = 'not requested';
    current.state = '1';
    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);
})();

 

 

 

shaik23_0-1728310418236.png

shaik23_0-1728310648477.png

 

@Ankur Bawiskar 

7 REPLIES 7

Anantha Gowrara
Kilo Sage

@ShaiK  you cannot restart cancelled flow but you can retrigger the flow by passing latest input values.

try {
var inputs = {};
inputs['req_item_sys_id'] = ; // GlideRecord of table:  
inputs['sc_req_item'] = 'incident';
 
              // Execute Synchronously: Run in foreground.
              // var timeout = ; //timeout in ms
             
              sn_fd.FlowAPI.executeFlow('flow_name', inputs);
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
 

Hello @Anantha Gowrara 

Actually I am not working for a specific catalog item, the flow name changes like if I select hardware request the flow shows hardware request if I select software it shows the flow name as software request etc 

So how can I put the name any alternative ways ?

Anantha Gowrara
Kilo Sage

@shaik23 If request has multiple items then you must first get the flow running on that RITM something like below

var requestGr = new GlideRecord('sc_request')

requestGr.addQuery('sys_id',current_request_id')

requestGr.query();

while(requestGr.next()){

var reqItem = new GlideRecord('sc_req_item');

reqItem.addQuery('sc_request',requestGr.sys_id);

reqItem.query();

while(reqItem.next()

var contextId = getContext(reqItemGr);

triggerFlow(contextId,reqItemGr);

}

 

function getContext(itemGr){

var contextId = new GlideRecord('sys_flow_context')

contextId.get('source_record',itemGr.sys_id);

return contextId.sys_id;

}

function triggerFlow(contextId,itemGr){

try {
var inputs = {};
inputs[itemGr.sys_id] = ; // GlideRecord of table:  
inputs['sc_req_item'] = 'incident';
 
              // Execute Synchronously: Run in foreground.
              // var timeout = ; //timeout in ms
             
              sn_fd.FlowAPI.executeFlow('contextId', inputs);
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}

}

 

@Anantha Gowrara  not multiple ritm  records the flow name changes every time like hard ware request, toner request like this

Check out the 2nd screen shot not it shows hardware request if I ordered toner it will show tone request as flow name