I currently use the following scripts to start a workflow.. But its seems that the workflow executes asynchronously in another thread . How I can use script to execute a workflow synchronously which has the same effect when I use "Workflow Element" to call a workflow?
var workflowToExecute = System.getModule("com.vmware.library.workflow").getWorkflowById(workflowId);
var objProperties = new Properties();
objProperties.put("paramter", parameterObject);
var objWorkflowToken= workflowToExecute.execute(objProperties);
Within your script, you need to wait until your token status has reached a completed or failed state before allowing your script to continue.
The function is my modification of the action "waitForCompletion" in com.vmware.library.workflow module
var workflowToExecute = System.getModule("com.vmware.library.workflow").getWorkflowById(workflowId);
var objProperties = new Properties();
objProperties.put("paramter", parameterObject);
var objWorkflowToken= workflowToExecute.execute(objProperties);
waitToComplete(objWorkflowToken);
function waitToComplete(currentToken)
{
var sleepWaitSeconds = 10;
var complete = false;
while(!complete)
{
if (currentToken != null && (currentToken.state != "running" && currentToken.state != "waiting" && currentToken.state != "waiting-signal" && currentToken.state != "completed"))
{
System.warn("Workflow '" + currentToken.name + "' terminated with status '" + currentToken.state + "'");
complete = true;
}
else if ((currentToken != null) && (currentToken.state == "completed"))
{
System.log("Workflow '" + currentToken.name + "' completed");
complete = true;
}
if (!complete)
System.sleep(sleepWaitSeconds * 1000);
}
}
I want both the launched workflow and the original workflow have the same WorkflowTokenId ,which is the behavior when I explicitly use "Workflow Element" to execute a workflow .
But if I use workflowInstance.execute(xxxxx) to launch a workflow , the launched workflow is another workflow instance which has a different WorkflowTokenId.
Thanks.
But i want the launched workflow to be the same workflow instance as the workflow of its caller . (i.e They execute in the same thread)
But the workflow launched by the workflowInstance.execute(xxxxx) will is another workflow instance.
In that case, I believe you have to "link" the workflow from within the original workflow. So you will most likely have to split your script code and use other elements (Decision, Custom Decision, etc.) to link the workflow.
That means call another workflow using "Workflow Element" cannot be done by the script now ?
If yes , I think it is very inconvenient for me to link which workflow to be called using GUI element. It is because there are about 100 possible conditions to call different workflows and the decision or custom decision element only allow the evaluation result to have two conditions now (i.e success or fail) . I think something like "switch case" decision element which allow to have many evaluation result will be helpful for me but I cannot find something like it in the vCO now.