
There are many scenarios where I need to trigger a workflow programmatically. In these cases, I must track its running state and determine the appropriate action for both success and failure. While moving to the next step after success is straightforward, handling failures is more complicated. In such instances, I may want to re-run the workflow using the original input parameters. But how can I accomplish this?
Let’s explore further.
Understanding the code
Contents
This simple JavaScript example demonstrates how to analyze workflow executions and selectively re-run them.
var allTokens = Server.findAllForType("WorkflowToken")
for each (token in allTokens) {
if (token.currentWorkflow.name === "aaa" && token.state != "running") {
token.currentWorkflow.execute(token.getInputParameters())
}
}
Re-run workflow code snippet
Step-by-step
Retrieving workflow tokens
var allTokens = Server.findAllForType("WorkflowToken")
Server.findAllForType("WorkflowToken")
retrieves all workflow tokens in vRO.- A workflow token represents a single execution (or run) of a workflow. Each token has metadata like the workflow name, inputs, state, and execution history.
- Here,
allTokens
becomes an array of all tokens currently known to the vRO server.
Iterating through tokens
for each (token in tokens) {
- Iterates through every workflow token found.
Filtering workflow executions
if (token.currentWorkflow.name === "aaa" && token.state != "running") {
- For each token (
token
), it checks two conditions:- The workflow’s name is exactly
"aaa"
. - The workflow execution (
token.state
) is not currently running (so the token represents a completed, failed, or canceled run).
- The workflow’s name is exactly
Re-executing the workflow
token.currentWorkflow.execute(token.getInputParameters())
- If both conditions are met, the code re-executes the workflow.
- It calls
.execute()
on the workflow object, passing in the same input parameters from the previous run (token.getInputParameters()
). - This effectively restarts the workflow
"aaa"
with the same inputs as before.
Why this might be useful
- Automatically restarting failed or completed workflows without manually kicking them off.
- Ensuring a workflow with specific inputs is always re-triggered whenever it stops.
- Testing purposes (e.g., repeatedly replaying the same workflow scenario).
💡
I also want