
There are numerous use-cases when I need to programmatically trigger a workflow. In such cases, I must monitor its running state and decide on the appropriate action in the event of success and failure. While it’s relatively straightforward to proceed to the next step upon success, the situation becomes more complex when faced with failure. In such scenarios, I’m likely to want to re-run the same workflow, utilizing the same input parameters that were provided initially. However, the question arises: how can I achieve this?
Let’s dive into the details.
Understanding the code
This simple code snippet demonstrates a JavaScript example that analyzes workflow executions and selectively re-runs 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 to hear from you about how I can improve my topics! Please leave a comment with the following:
– Topics you’re interested in for future editions
– Your favorite part or take away from this one
I’ll do my best to read and respond to every single comment!