FlowAPI - Scoped, Global
-
- UpdatedAug 1, 2024
- 30 minutes to read
- Xanadu
- API reference
The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.
Access FlowAPI methods in global and scoped scripts using the sn_fd
namespace. Create calls to your flows using the Code Snippet action in Workflow Studio or use the methods detailed here to update scripts manually.
FlowAPI - cancel(String contextId, String reason)
Cancels a paused or running flow, subflow, or action.
Name | Type | Description |
---|---|---|
contextId | String | Sys_id of the execution details record for the flow, subflow, or action. Access the execution details by navigating to the Flow Executions tab in Workflow Studio, or pass the sys_id of the context record returned by the startFlow(), startSubflow(), or startAction() methods. Note: There might be a small delay in cancellation if
the target context is running on a different node of the instance. Contexts running on the
same node are cancelled immediately. Contexts running on another node must first wait for
the cancellation to be forwarded to the appropriate node. |
reason | String | Optional. Reason for canceling the flow, subflow, or action. Appears in the Message field of the Flow engine log entries [sys_flow_log] table. |
Type | Description |
---|---|
void |
Example
This example uses the return value of the startFlow() method to cancel any long-running flows.
Example
This example cancels any flows named Test Flow.
FlowAPI - executeAction(String name, Map inputs, Number timeout)
Runs an action from a server-side script synchronously.
Name | Type | Description |
---|---|---|
name | String | The scope and name of the action to be executed, for example
global.action_name . |
inputs | Map | Name-value pairs that define action inputs. Use the input name, not the input label. |
timeout | Number | Optional. Timeout in milliseconds. This value overrides the 30 second default timeout specified by the com.glide.hub.flow_api.default_execution_time system property. After the timeout expires, an exception is thrown. |
Type | Description |
---|---|
Object | The action outputs. |
Example
In this example, the script uses sn_fd.FlowAPI.executeAction to execute an action called actionforpassword2test in the global scope. A variable called inputs contains the inputs for the action. In this case, a name and password. The outputs for the action are stored in the outputs variable, which in this case, is an encrypted password object. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
FlowAPI - executeActionQuick(String name, Map inputs, Number timeout)
Run an action from a server-side script synchronously from the current user session without creating execution details or other related records. Improve performance by eliminating record-keeping overhead. Use this API to increase the speed of high-volume processing, for example multiple executions per second, in a production environment.
- Reporting & records generated
- This method does not create execution details and context records, regardless of Workflow Studio settings.
- Wait support
- This method does not support pausing the action to wait for conditions. Steps that pause for wait conditions such as Ask for Approval or Wait for Condition are not supported.
- MID Server support
- This method does not support pausing an action to run from a MID Server.
Name | Type | Description |
---|---|---|
name | String | Scope and internal name of the action to execute. For example,
global.action_name . Locate the Internal
name field in the list of Workflow Studio
actions. |
inputs | Map | Name-value pairs that define action inputs. You can find the available action
inputs and required data types under Inputs in the action outline. Use the input
name, not the input label. For example,
{'table':'incident','sys_id':'a39d8e3cf0212300964feeefe80ff0ed'} . |
timeout | Number | Optional. Timeout in milliseconds. This value overrides the 30 second default timeout specified by the com.glide.hub.flow_api.default_execution_time system property. After the timeout expires, an exception is thrown. |
Type | Description |
---|---|
Object | Object containing outputs defined by the action. You can find the outputs for the action under Outputs in the action outline. |
Example
FlowAPI - executeDataStreamAction(String name, Map inputs, Number timeout)
Runs a Data Stream action synchronously from a server-side script and returns a ScriptableDataStream object.
For more information about Data Stream actions, see Data Stream actions and pagination.
try/catch
block to catch errors. Always include a
finally
statement that ends with the close()
method from the ScriptableDataStream class to close the data
stream and prevent performance issues.Name | Type | Description |
---|---|---|
name | String | The scope and name of the Data Stream action to execute. For example,
global.data_stream_action_name . |
inputs | Map | Name-value pairs that define action inputs. Use the input name, not the input label. If the action does not have any inputs, do not include this parameter. |
timeout | Number | Optional. Amount of time before the action times out. After the timeout
expires, an exception is thrown. The timeout only applies to the
executeDataStreamAction method, not to methods in the
ScriptableDataStream class. Default: 30000, specified by the com.glide.hub.flow_api.default_execution_time system property Unit: Milliseconds |
Type | Description |
---|---|
ScriptableDataStream | An object used to iterate through items in the data stream. Use the methods in the ScriptableDataStream class to interact with this object. See ScriptableDataStream API. |
Example
This example creates an incident record for each item returned in the data stream.
FlowAPI - executeFlow(String name, Map inputs, Number timeout)
Runs a flow from a server-side script synchronously.
Name | Type | Description |
---|---|---|
name | String | The scope and name of the flow to be executed, for example
global.flow_name . |
inputs | Map | Name-value pairs that define trigger inputs. Use the input name, not the input label. |
timeout | Number | Optional. Timeout in milliseconds. This value overrides the 30 second default timeout specified by the com.glide.hub.flow_api.default_execution_time system property. After the timeout expires, an exception is thrown. |
Type | Description |
---|---|
None | Normal operation produces no return value. |
Exception | The API throws an exception when a flow called synchronously pauses.
The current execution is in the waiting
state . In most cases, the exception is removed when the flow resumes.
However, the API cannot resume a flow that has been sent to a MID
Server. |
Example
This example uses sn_fd.FlowAPI.executeFlow to execute a global flow called test_flow. This flow is normally triggered when a record on the incident table is updated. Because you are activating the flow from a script, you must provide this information. The code creates an inputs variable that contains the current record, and the table for the record. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
FlowAPI - executeFlowQuick(String name, Map inputs, Number timeout)
Runs a flow, subflow, action, or Data Stream action from a server-side script synchronously or asynchronously without creating execution details or other related records. Improves performance by eliminating record-keeping overhead. Use this API to increase the speed of high-volume processing, for example multiple executions per second, in a production environment.
- Reporting & records generated
- This method does not create execution details and context records, regardless of Workflow Studio settings.
- Run as user
- This method runs the flow as the user who initiates the session. Setting the flow to run as the system user, or impersonating a user, is not supported.
- Wait support
- This method does not support pausing the flow to wait for conditions. Actions or flow logic that pause for wait conditions such as Ask for Approval, Wait for Condition, or Wait for a duration are not supported.
- MID Server support
- This method does not support pausing a flow to run from a MID Server.
Name | Type | Description |
---|---|---|
name | String | Scope and internal name of the flow to execute. For example,
global.flow_name . Locate the Internal name
field in the list of Workflow Studio
flows. |
inputs | Map | Name-value pairs that define trigger inputs. You can find the available trigger
inputs and required data types in the Trigger section of the flow. Use the input
name, not the input label. For example,
{'table':'incident','sys_id':'a39d8e3cf0212300964feeefe80ff0ed'} . |
timeout | Number | Optional. Timeout in milliseconds. This value overrides the 30 second default timeout specified by the com.glide.hub.flow_api.default_execution_time system property. After the timeout expires, an exception is thrown. |
Type | Description |
---|---|
void |
Example
FlowAPI - executeSubflow(String name, Map inputs, Number timeout)
Runs an subflow from a server-side script synchronously.
Name | Type | Description |
---|---|---|
name | String | The scope and name of the subflow to be executed, for example
global.subflow_name . |
inputs | Map | Name-value pairs that define subflow inputs. Use the input name, not the input label. |
timeout | Number | Optional. Timeout in milliseconds. This value overrides the 30 second default timeout specified by the com.glide.hub.flow_api.default_execution_time system property. After the timeout expires, an exception is thrown. |
Type | Description |
---|---|
Object | Object containing the subflow outputs. |
Exception | The API throws an exception when a flow called synchronously pauses.
The current execution is in the waiting
state . In most cases, the exception is removed when the flow resumes.
However, the API cannot resume a flow that has been sent to a MID
Server. |
Example
In this example, the script uses sn_fd.FlowAPI.executeSubflow to execute an subflow called subflowTest in the global scope. A variable called inputs contains the inputs for the subflow. In this case, a name and password. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
FlowAPI - executeSubflowQuick(String name, Map inputs, Number timeout)
Run a subflow from a server-side script synchronously from the current user session without creating execution details or other related records. Improve performance by eliminating record-keeping overhead. Use this API to increase the speed of high-volume processing, for example multiple executions per second, in a production environment.
- Reporting & records generated
- This method does not create execution details and context records, regardless of Workflow Studio settings.
- Run as user
- This method runs the flow as the user who initiates the session. Setting the flow to run as the system user, or impersonating a user, is not supported.
- Wait support
- This method does not support pausing the flow to wait for conditions. Actions or flow logic that pause for wait conditions such as Ask for Approval, Wait for Condition, or Wait for a duration are not supported.
- MID Server support
- This method does not support pausing a flow to run from a MID Server.
Name | Type | Description |
---|---|---|
name | String | Scope and internal name of the subflow to execute. For example,
global.subflow_name . Locate the Internal
name field in the list of Workflow Studio
subflows. |
inputs | Map | Name-value pairs that define subflow inputs. You can find the available subflow
inputs and required data types under Inputs in the subflow. Use the input name, not
the input label. For example,
{'table':'incident','sys_id':'a39d8e3cf0212300964feeefe80ff0ed'} . |
timeout | Number | Optional. Timeout in milliseconds. This value overrides the 30 second default timeout specified by the com.glide.hub.flow_api.default_execution_time system property. After the timeout expires, an exception is thrown. |
Type | Description |
---|---|
Object | Object containing outputs defined by the subflow. You can find the outputs for the subflow under Subflow Inputs & Outputs in the subflow outline. |
Example
FlowAPI - getErrorMessage(String contextId)
Returns the error messages produced by a flow, subflow, or action. This method cannot return messages from flows, subflows, or actions run with the quick() API.
Name | Type | Description |
---|---|---|
contextId | String | The sys_id of the flow, subflow, or action whose error messages you want to get. |
Type | Description |
---|---|
String | The last operation run and the error message it produced. |
Example
This example starts a global subflow called test_error_subflow and returns any error messages it produces. Normally, a single script does not both run a flow and then get its error messages. Typically, either another script or Workflow Studio would have already run the flow. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
Output:
FlowAPI - getFlowStages(String scopedFlowName)
Return a JSON string containing the list of flow stages.
Name | Type | Description |
---|---|---|
scopedFlowName | String | The application scope and name of the flow whose stages you want to get. |
Type | Description |
---|---|
String | JSON formatted string that lists the stages of the flow by their stage record values. |
Example
This example shows getting the stages for the Service Catalog Item Request flow.
Output:
The JSON string contains name-value pairs for a stage field. See for more information.
FlowAPI - getOutputs(String contextId)
Returns the outputs of a completed action or subflow.
Name | Type | Description |
---|---|---|
contextId | String | The sys_id of the action or subflow whose outputs you want to get. |
Type | Description |
---|---|
Object | Object containing the action or subflow outputs. |
Example
This example starts a global subflow called test_subflow, waits for the flow to finish, and then gets its output values. Normally, a single script does not both run a flow and then get its output values. Since a flow may not complete before the getOutputs() call, this example uses a wait time. Typically, either another script or Workflow Studio would have already run the flow. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
FlowAPI - getRunner()
Returns a ScriptableFlowRunner builder object for a flow or action that you want to run.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
ScriptableFlowRunner | Builder object used to run a Workflow Studio action, flow, or subflow. |
Example
This example runs a flow synchronously.
FlowAPI - getStatus(String contextId)
Returns the status of a flow, subflow, or action.
Name | Type | Description |
---|---|---|
contextId | String | The sys_id of the flow, subflow, or action whose status you want to get. |
Example
This example runs a flow containing Do the following in parallel flow logic. Normally, a single script does not both run a flow and then get its error messages. Typically, either another script or Workflow Studio would have already run the flow.
FlowAPI - hasApprovals(String scopedFlowName)
Checks if a flow within a give scope contains any Ask for Approval actions.
The hasApprovals() method determines if a flow within a given scope contains any Ask for Approval actions. This method also checks if any Ask for Approval actions within the flow are nested under If flow logic blocks. For more information, see Ask for Approval actions.
Name | Type | Description |
---|---|---|
scopedFlowName | String | Scope and internal name of the flow to execute. For example,
global.flow_name . Locate the Internal name
field in the list of Workflow Studio flows. |
Type | Description |
---|---|
String | Returns one of the following string values:
|
Example
The following example checks if the example_flow flow within the global scope contains any Ask for Approval actions and logs the result.
FlowAPI - restartFlowFromContext(String ContextId, Map providedInputs)
Restarts a flow, subflow, or action that was run in the background. You can provide new inputs or omit them to reuse the previously provided inputs.
Name | Type | Description |
---|---|---|
contextId | String | The sys_id of the flow, subflow, or action you want to restart. This flow, subflow, or action must have run in background and have an associated sysevent record. |
providedInputs | Object | Optional. Map object containing the name-value pairs that define replacement inputs for the flow, subflow, or action. If you omit or provide a null value for this parameter, the flow, subflow, or action runs using the previously provided inputs. |
Type | Description |
---|---|
ScriptableFlowRunnerResultsScoped | Object containing the execution details of a Workflow Studio action, flow, or subflow. |
Example
Restarts a sample flow that has a record-based trigger on the Incident table. The original flow used sample incident INC0008112 as an input. When the flow restarts it instead uses sample incident INC0008111 as an input.
FlowAPI - scheduleCancel(String contextId, String reason, Integer delaySeconds)
Schedule a system event in the Flow Engine Queue to cancel a flow that is in the in progress, presumed interrupted, or waiting state after a delay.
Name | Type | Description |
---|---|---|
contextId | String | Sys_id of the execution details record for the flow, subflow, or action. The
target flow, subflow, or action must be in the in progress, presumed interrupted, or
waiting state. Access the execution details by navigating to the Flow Executions tab
in Workflow Studio, or pass the sys_id of the context record returned
by the method. Note: There might be a small delay in cancellation if
the target context is running on a different node of the instance. Contexts running on the
same node are cancelled immediately. Contexts running on another node must first wait for
the cancellation to be forwarded to the appropriate node. |
reason | String | Reason for canceling the flow, subflow, or action. Appears in the Message field of the Flow engine log entries [sys_flow_log] table. |
delaySeconds | Integer | Optional. Number of seconds to wait before processing a cancellation event in the
Flow Engine Queue. If you omit this parameter, the method uses the default value of
1 second. You can use this parameter to avoid the performance impact of scheduling the cancellation of thousands of flow contexts at the same time. Rather than run all cancellations simultaneously, schedule a batch of flow cancellations with a delay. Alternatively, you could use the delay as a time out value for an asynchronous flow. |
Type | Description |
---|---|
None |
Example
This example uses the return value of the startFlow() method to schedule the cancellation of any long-running flows.
Example
This example schedules the cancellation of any flows named Test Flow.
Example
This example uses the delaySeconds parameter to run flow cancellation jobs in batches. Use batches to cancel thousands of flow contexts.
FlowAPI - setEncryptedOutput(String password)
Builds password2 values inside a script step.
Identify an encrypted password2 value returned from a GlideRecord, enabling the system to display the value as a masked password rather than an encrypted string.
Name | Type | Description |
---|---|---|
password | String | Encrypted password2 value. |
Type | Description |
---|---|
String | Encrypted password2 value, recognised by the engine as a password value. |
Example
This example returns value of a password2 field from a user record and stores in in a variable. This variable is passed into the setEncryptedOutput method, which is called using sn_fd.GlideActionUtil.setEncryptedOutput. The instance recognises the returned value as a password.
FlowAPI - startAction(String name, Map inputs)
Runs an action from a server-side script asynchronously.
Name | Type | Description |
---|---|---|
name | String | The scope and name of the action to be executed, for example
global.action_name . |
inputs | Map | Name-value pairs that define action inputs. Use the input name, not the input label. |
Type | Description |
---|---|
String | Sys Id of the context record for the action. Access the context record by navigating to the Flow Executions tab in Workflow Studio, selecting a flow execution, and clicking Open Context Record. |
Example
This example uses sn_fd.FlowAPI.startAction to execute an action called add_comment in the sn_itsm_spoke scope. The inputs object contains a target record and a comment to add to that record. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
FlowAPI - startActionQuick(String name, Map inputs)
Runs an action from a server-side script asynchronously without creating execution details or other related records. Improve performance by eliminating record-keeping overhead. Use this API to increase the speed of high-volume processing, for example multiple executions per second, in a production environment.
- Reporting & records generated
- This method does not create execution details and context records, regardless of Workflow Studio settings.
- Wait support
- This method does not support pausing the action to wait for conditions. Steps that pause for wait conditions such as Ask for Approval or Wait for Condition are not supported.
- MID Server support
- This method does not support pausing an action to run from a MID Server.
Name | Type | Description |
---|---|---|
name | String | Scope and internal name of the action to execute. For example,
global.action_name . Locate the Internal
name field in the list of Workflow Studio
actions. |
inputs | Map | Name-value pairs that define action inputs. You can find the available action
inputs and required data types under Inputs in the action outline. Use the input
name, not the input label. For example,
{'table':'incident','sys_id':'a39d8e3cf0212300964feeefe80ff0ed'} . |
Type | Description |
---|---|
void |
Example
FlowAPI - startFlow(String name, Map inputs)
Runs a flow from a server-side script.
Name | Type | Description |
---|---|---|
name | String | The scope and name of the flow to be executed, for example
global.flow_name . |
inputs | Map | Name-value pairs that define trigger inputs. Use the input name, not the input label. |
Type | Description |
---|---|
String | Sys Id of the context record for the flow. Access the context record by navigating to the Flow Executions tab in Workflow Studio, selecting a flow execution, and clicking Open Context Record. |
Example
This example uses sn_fd.FlowAPI.startFlow to execute a global flow called test_flow. The code creates an inputs variable that contains inputs required by the flow. In this case, the current record and the table for the record. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
FlowAPI - startFlowQuick(String name, Map inputs)
Runs a flow from a server-side script asynchronously without creating execution details or other related records. Improve performance by eliminating record-keeping overhead. Use this API to increase the speed of high-volume processing, for example multiple executions per second, in a production environment.
- Reporting & records generated
- This method does not create execution details and context records, regardless of Workflow Studio settings.
- Run as System user
- This method runs the flow as the System user. Setting the flow to run as the user who initiates the session, or impersonating a user, is not supported.
- Wait support
- This method does not support pausing the flow to wait for conditions. Actions or flow logic that pause for wait conditions such as Ask for Approval, Wait for Condition, or Wait for a duration are not supported.
- MID Server support
- This method does not support pausing a flow to run from a MID Server.
Name | Type | Description |
---|---|---|
name | String | Scope and internal name of the flow to execute. For example,
global.flow_name . Locate the Internal name
field in the list of Workflow Studio
flows. |
inputs | Map | Name-value pairs that define trigger inputs. You can find the available trigger
inputs and required data types in the Trigger section of the flow. Use the input
name, not the input label. For example,
{'table':'incident','sys_id':'a39d8e3cf0212300964feeefe80ff0ed'} . |
Type | Description |
---|---|
void |
Example
FlowAPI - startSubflow(String name, Map input)
Runs a subflow from a server-side script.
Name | Type | Description |
---|---|---|
name | String | The scope and name of the subflow to be executed, for example
global.subflow_name . |
inputs | Map | Name-value pairs that define subflow inputs. Use the input name, not the input label. |
Type | Description |
---|---|
String | Sys Id of the context record for the subflow. Access the context record by navigating to the Flow Executions tab in Workflow Studio, selecting a flow execution, and clicking Open Context Record. |
Example
This example uses sn_fd.FlowAPI.startSubflow to execute a global flow called test_subflow. The code is wrapped in a try/catch statement to capture any errors that might occur when the flow executes.
FlowAPI - startSubflowQuick(String name, Map inputs)
Runs a subflow from a server-side script asynchronously without creating execution details or other related records. Improve performance by eliminating record-keeping overhead. Use this API to increase the speed of high-volume processing, for example multiple executions per second, in a production environment.
- Reporting & records generated
- This method does not create execution details and context records, regardless of Workflow Studio settings.
- Run as System user
- This method runs the flow as the System user. Setting the flow to run as the user who initiates the session, or impersonating a user, is not supported.
- Wait support
- This method does not support pausing the flow to wait for conditions. Actions or flow logic that pause for wait conditions such as Ask for Approval, Wait for Condition, or Wait for a duration are not supported.
- MID Server support
- This method does not support pausing a flow to run from a MID Server.
Name | Type | Description |
---|---|---|
name | String | Scope and internal name of the subflow to execute. For example,
global.subflow_name . Locate the Internal
name field in the list of Workflow Studio
subflows. |
inputs | Map | Name-value pairs that define subflow inputs. You can find the available subflow
inputs and required data types under Inputs in the subflow. Use the input name, not
the input label. For example,
{'table':'incident','sys_id':'a39d8e3cf0212300964feeefe80ff0ed'} . |
Type | Description |
---|---|
void |
Example
On this page
- FlowAPI - cancel(String contextId, String reason)
- FlowAPI - executeAction(String name, Map inputs, Number timeout)
- FlowAPI - executeActionQuick(String name, Map inputs, Number timeout)
- FlowAPI - executeDataStreamAction(String name, Map inputs, Number timeout)
- FlowAPI - executeFlow(String name, Map inputs, Number timeout)
- FlowAPI - executeFlowQuick(String name, Map inputs, Number timeout)
- FlowAPI - executeSubflow(String name, Map inputs, Number timeout)
- FlowAPI - executeSubflowQuick(String name, Map inputs, Number timeout)
- FlowAPI - getErrorMessage(String contextId)
- FlowAPI - getFlowStages(String scopedFlowName)
- FlowAPI - getOutputs(String contextId)
- FlowAPI - getRunner()
- FlowAPI - getStatus(String contextId)
- FlowAPI - hasApprovals(String scopedFlowName)
- FlowAPI - restartFlowFromContext(String ContextId, Map providedInputs)
- FlowAPI - scheduleCancel(String contextId, String reason, Integer delaySeconds)
- FlowAPI - setEncryptedOutput(String password)
- FlowAPI - startAction(String name, Map inputs)
- FlowAPI - startActionQuick(String name, Map inputs)
- FlowAPI - startFlow(String name, Map inputs)
- FlowAPI - startFlowQuick(String name, Map inputs)
- FlowAPI - startSubflow(String name, Map input)
- FlowAPI - startSubflowQuick(String name, Map inputs)