Flow Designer | Using FlowAPI to cancel or start the flow

Опубликовано: 29 Сентябрь 2024
на канале: ServiceNow Universe
825
16

Flow Designer | Cancel or Start the flow using script | Using FlowAPI to cancel or start the flow

1. Canceling a Flow using Script:
To cancel a running flow programmatically, you can use the sn_fd.FlowAPI API in a server-side script. This API allows you to locate and cancel a specific flow context based.

2. Starting a Flow using Script:
To start a flow programmatically from a script, you can use the sn_fd.FlowAPI in a server-side script. This API allows you to trigger a flow and pass input data to it.

Change Approver UI Action Script:
------------------------------------------------------
current.update(); //Save current record

//Cancel the existing approval requests
var approver = new GlideRecord("sysapproval_approver");
approver.addQuery("sysapproval="+current.sys_id.toString());
approver.orderByDesc("sys_created_on");
approver.query();

while(approver.next()){
approver.state = "cancelled";
approver.update();
}

//Get existing flow context
var grFlowContext = new GlideRecord("sys_flow_context");
grFlowContext.addQuery("source_record=" + current.sys_id.toString());
grFlowContext.addQuery("name=ServiceNow Universe Test Catalog Flow"); //name of the flow
grFlowContext.orderByDesc("sys_created_on");
grFlowContext.query();

if (grFlowContext.next()) {
//Cancel existing flow context
sn_fd.FlowAPI.cancel(grFlowContext.sys_id.toString(), 'Changing approver');
}

try {
//Create inputs object
var inputs = {};
inputs['request_item'] = current;
inputs['table_name'] = "sc_req_item";

//Trigger the flow again
var contextId = sn_fd.FlowAPI.startFlow('global.servicenow_universe_test_catalog_flow', inputs);

//Set the source table and source record fields in flow context record
var grFlowContext = new GlideRecord("sys_flow_context");
if (grFlowContext.get(contextId)) {
grFlowContext.source_table = "sc_req_item";
grFlowContext.source_record = current.sys_id.toString();
grFlowContext.update();
}

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

action.setRedirectURL(current);
---------------------------------------------------------------------------------------------------------------------------------

#servicenowadmin #servicenowimplementation #flowdesiner