arshanapalli
Tera Contributor

Hi Community,

In recent times I had a requirement to restart/retrigger a flow designer, the use case was to cancel the existing flow when the date field gets modified(there was a wait for condition on that date field). So when the date gets updated by fulfiller the existing flow should get cancelled and the new flow show start with the updated date. This use case was used in employee termination process.

So I have achieved this by a business rule. 

When to run: After update on the catalog item. 

Advanced condition: current.variables.'variable_name'.changes()

Note: Please update the variable_name to the variable name. 

------------------------------------------------------------------------------------------------------------------

Script:

(function executeRule(current, previous /*null when async*/ ) {

var now_GR = new GlideRecord("sys_flow_context");
now_GR.addQuery("name", "xxxxxx");// flow designer name
now_GR.addQuery("source_record="+current.sys_id);
now_GR.query();

while (now_GR.next()) {
sn_fd.FlowAPI.cancel(now_GR.getUniqueValue(), 'Canceling xxxxxxx'); // flow designer name
}

startFlowDesignerFlow(current);

function startFlowDesignerFlow(current) {
var flow = current.cat_item.flow_designer_flow;
var flowName = flow.sys_scope.scope + "." + flow.internal_name;

sn_flow_trigger.FlowTriggerAPI.fireCatalogTrigger(flowName, current);
}

})(current, previous);

------------------------------------------------------------------------------------------------------------------

This restart/retrigger flow designer can also be used in different scenario's.

 

Comments
hanaphouse
Giga Guru

Has anyone successfully restarted / retriggered a flow?

NBeheydt
Tera Expert

I have not.  I've read through all the different posts and the API documentation and have not been able to get a flow on a catalog item to restart.

Kim Sullivan
Tera Guru

No -- I've even tried the script here and it doesn't work.

Rajesh80
Tera Contributor

Did anyone has any updates on How to restart flow designer based on certain conditions .

Nikesh A
ServiceNow Employee
ServiceNow Employee
 try {
            var inputs = {};
            inputs['table_name'] = 'sc_req_item';
            inputs['request_item'] = current; 

            // Start Asynchronously: Uncomment to run in background.
            sn_fd.FlowAPI.getRunner().flow('global.test').inBackground().withInputs(inputs).run();

            // Execute Synchronously: Run in foreground.
            //sn_fd.FlowAPI.getRunner().flow('global.test').inForeground().withInputs(inputs).run();

        } catch (ex) {
            var message = ex.getMessage();
            gs.error(message);
        }





Give it a try with above script
CEwing
Tera Explorer

This doesn't work as the flow context will be a UI Action and not the actual item.

Ana Carolina1
Tera Explorer

The following script successfully deletes associated tasks and approvers, then restarts the Flow Designer for each specified RITM (Requested Item).

 

// Retrieve RITM records and start Flow Designer for each one
var current = new GlideRecord("sc_req_item");
current.setWorkflow(false); // Ignore Business Rules
current.addQuery('number', 'IN', 'XXXXX'); // Query by RITM number
current.query();

while (current.next()) {
// Delete existing approvers and tasks for the current RITM
deletApprovers(current.sys_id);
deletTask(current.sys_id);

try {
// Retrieve the Flow Designer flow linked to the catalog item
var flow = current.cat_item.flow_designer_flow;
var flowName = flow.sys_scope.scope + "." + flow.internal_name; // Construct flow name with scope

// Prepare input parameters for the flow
var inputs = {};
inputs['table_name'] = 'sc_req_item';
inputs['request_item'] = current;

// Start the flow asynchronously (uncomment to run in background)
// sn_fd.FlowAPI.getRunner().flow('global.test').inBackground().withInputs(inputs).run();

// Execute the flow synchronously (foreground execution)
sn_fd.FlowAPI.getRunner().flow(flowName).inForeground().withInputs(inputs).run();

} catch (ex) {
// Log error message if the flow fails to start
var message = ex.getMessage();
gs.error("ERROR >>> " + message);
}
}

// Function to delete approvers linked to the given RITM ID
function deletApprovers(currentID) {
var grDelete = new GlideRecord('sysapproval_approver');
grDelete.addQuery("sysapproval", currentID); // Find approvers related to the RITM
grDelete.query();
grDelete.deleteMultiple(); // Delete all matching approvers
}

// Function to delete tasks linked to the given RITM ID
function deletTask(currentID) {
var grDelete = new GlideRecord('sc_task');
grDelete.addQuery("request_item", currentID); // Find tasks related to the RITM
grDelete.query();
grDelete.deleteMultiple(); // Delete all matching tasks
}

 

Version history
Last update:
‎12-23-2020 02:00 PM
Updated by: