VMware Cloud Community
kc987654321
Contributor
Contributor

How can use script to invoke a workflow synchronously ?

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);

Tags (2)
0 Kudos
6 Replies
Burke-
VMware Employee
VMware Employee

Within your script, you need to wait until your token status has reached a completed or failed state before allowing your script to continue.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
0 Kudos
robrtb12
Enthusiast
Enthusiast

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);

  }

}

0 Kudos
kc987654321
Contributor
Contributor

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.

0 Kudos
kc987654321
Contributor
Contributor

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.

0 Kudos
robrtb12
Enthusiast
Enthusiast

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.

0 Kudos
kc987654321
Contributor
Contributor

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.

0 Kudos