A server-side script can start a Flow Designer flow, subflow or action through the FlowAPI in the sn_fd namespace. Use a synchronous call when the script must wait for the run to finish. If the script also needs outputs, use executeSubflow or executeAction, since executeFlow returns no value. Use an asynchronous call when it can check the run later using the context ID it returns. Timeouts, quick variants and cancellation follow.
Which scripts can call the FlowAPI
The FlowAPI is built for server-side scripts. Methods are reached through the sn_fd namespace in both global and scoped applications, so a call looks like sn_fd.FlowAPI followed by the method name.
The API covers three kinds of object: flows, subflows and actions. A subflow has inputs and outputs, a sequence of actions and flow logic, and no trigger, so other flows or scripts call it. An action is a reusable operation. The method name tells you which kind you are running, so a flow is started with a flow method, a subflow with a subflow method, and a single action with an action method.
Workflow Studio can generate these calls for a given flow through its Code Snippet action. Writing the call by hand works too, and the generated version is usually the quicker route to the correct names.
Synchronous calls with executeFlow
executeFlow takes three arguments: the flow name, a map of inputs and an optional timeout in milliseconds. It runs the flow in the current request and returns no value, so the calling script cannot read outputs from it. The flow runs as the user set in the flow’s properties, which is not necessarily the user who triggered the script.
If the script needs outputs, use executeSubflow or executeAction instead. executeSubflow returns an object holding the subflow’s outputs. executeAction runs as the session user and returns an object holding the action’s outputs. Both are synchronous and take the same optional timeout.
A synchronous call that reaches a pause, such as an Ask for Approval or a Wait for Condition step, throws an exception. A flow sent to a MID Server cannot be resumed from inside the calling script either. Plan for that exception in any script that calls a flow whose design includes a wait.
The timeout
The timeout parameter overrides the default. The default comes from the system property com.glide.hub.flow_api.default_execution_time and is 30 seconds. When the timeout expires, an exception is thrown. Set the value to match how long the flow takes in practice, and wrap the call in try and catch so the script can record the failure instead of stopping without a trace.
executeDataStreamAction is the synchronous method for data stream actions. Its default timeout is 30000 milliseconds, and it returns a ScriptableDataStream. Wrap that call in try and catch as well, and call close() in a finally block so the stream is released whether or not the call succeeds.
Asynchronous calls with startFlow
startFlow, startSubflow and startAction run in the background. Each returns a context ID, which is a sys_id, and the calling script moves on without waiting. Flows and subflows run as the user set in their properties. Actions started this way run as the session user, and their outputs are not available to the calling script.
Because the script cannot read outputs from an asynchronous run directly, the context ID is the handle you keep. getOutputs takes that ID and returns the outputs of a completed action or subflow. A common pattern is to store the context ID on the record that started the run, then check it later from a scheduled job or a second script.
Checking status, errors and cancellation
getStatus takes a context ID and returns an object with three fields: hasLookupError, a Boolean; errorMessage, a string; and contextStatus, an object. Read the run’s state from contextStatus.state, which can be COMPLETE, ERROR, CANCELLED, IN_PROGRESS or WAITING. Branch on that state rather than guessing from timing.
Each state carries its own detail. A COMPLETE run reports an errorState of ERROR_CAUGHT or ERROR_SKIPPED. An ERROR run includes a message. A CANCELLED run includes a canceledReason. An IN_PROGRESS run reports isRunOnMid. A WAITING run includes waitingOn, which gives the order and step type of the paused step, along with step-specific fields such as the table, condition, end time or approval rule.
getErrorMessage takes a context ID and returns the error message as a string. It cannot return messages for quick runs, because quick runs create no records.
cancel takes a context ID and an optional reason. The reason is written to the message of the flow log, which records why the run stopped. A cancellation sent to a run on another node can take effect with a delay. scheduleCancel takes a context ID, a reason and an optional number of seconds to wait before cancelling. That delay defaults to one second. scheduleCancel applies to runs that are in progress, presumed interrupted, or waiting.
Three more methods help with flow design. getFlowStages returns a JSON string listing a flow’s stages. hasApprovals reports whether a flow always contains approvals, contains them only conditionally inside an If block, never contains them, or returns UNKNOWN when the compiler reports an error. sendMessage resumes a flow that is paused at a Wait for Message action. restartFlowFromContext restarts a flow that ran in the background, optionally with new inputs, and it requires an associated sysevent record.
The quick variants and their limits
The quick methods, which include executeFlowQuick, startFlowQuick, executeActionQuick and startSubflowQuick, run a flow, subflow or action without creating execution details or context records. That keeps the instance lighter, and it also removes the trail.
The limits follow from the lack of records. The quick variants do not support wait steps. executeFlowQuick also does not support running a flow from a MID Server. executeFlowQuick and executeSubflowQuick run as the session user, and startFlowQuick and startSubflowQuick run as the System user. Changing the run-as user, or impersonating a user, is not supported for these variants. The quick variants return nothing, except executeActionQuick and executeSubflowQuick, which return outputs.
Use a quick variant when you need a fast run that nobody has to inspect later. Use the standard method when the run needs a record that someone can open and audit.
Avoid the call inside an asynchronous business rule
Avoid calling these methods from an asynchronous business rule, because it can hurt instance performance. Create a scheduled job record in Workflow Studio instead. A scheduled job starts the flow on a schedule, which moves the work out of the request path and puts the timing in one place.
Domain-separated instances and the ScriptableFlowRunner builder
On a domain-separated instance, a flow, subflow or action started through the FlowAPI runs in the domain of the user who started the script.
Several of the older methods are replaced by ScriptableFlowRunner – Scoped. You get a builder from sn_fd.FlowAPI.getRunner(), which takes no arguments. The builder is chained. flow() or action() names the target, inForeground() or inBackground() sets where it runs, withInputs() passes the inputs, and inDomain() picks a specific domain. run() starts the work and returns a result object with getContextId(), getDomainId(), getFlowObjectName(), getFlowObjectType(), getOutputs() and getDate().
The builder is the variant to use when a run must support domain separation. The older methods are deprecated in favour of the builder, so existing scripts that do not need domain separation can keep using them.
Frequently asked questions
Can a client script start a flow directly?
FlowAPI methods run in server-side scripts. A client script that needs a flow to run would typically ask a server-side script to make the call.
What happens if the flow takes longer than the timeout?
An exception is thrown when the timeout expires. Catch it in the script, and use getStatus with the context ID, where one exists, to read the state of the run.
Why does executeFlow return nothing?
It is the synchronous flow method, and it returns no value. Use executeSubflow or executeAction when the script needs outputs from a synchronous run.
How do I find out why a background run failed?
Keep the context ID that startFlow returned, then call getStatus to read the state and getErrorMessage to read the last error. Quick runs create no records, so getErrorMessage cannot return their messages.
Should I call the FlowAPI from a business rule?
Avoid it in an asynchronous business rule. Create a scheduled job record in Workflow Studio for the call instead.
When should I use the ScriptableFlowRunner builder?
Use it when the run needs to target a specific domain on a domain-separated instance. It is the variant built for domain separation.