vRealize Orchestrator: Invoke a workflow using Javascript in an action/scriptable task

Calling a workflow in another workflow in vRealize Orchestrator (vRO) is very straightforward. You can just search for the workflow and drag to the correct location in your master workflow. But it is also possible to invoke a workflow via Javascript code. This blogpost will give a simple example of how you can call a workflow from another workflow using a scriptable task. Please note that the same code can be used to invoke a workflow from an action.

For this blogpost I created two workflows:

  • mainWorkflow: the workflow that will be source of the trigger
  • invokedWorkflow: the workflow that will be triggered

Screen Shot 2015-11-04 at 16.32.04

The ‘invokedWorkflow’ workflow is a very simple one. It has two input parameters (stringInput and vmInput) and one output parameter (stringOutput). The goal of the workflow is to take the two input parameters and to convert It to a single string (the output parameter).

Code of ‘invokedWorkflow’:

Screen Shot 2015-11-04 at 15.42.01

stringOutput = 'Following variables were passed to the workflow: stringInput='+stringInput+', vmInput (name)='+ vmInput.name 
System.log(stringOutput)

The ‘mainWorkflow’ workflow will invoke ‘invokedWorkflow’, pass the needed input parameters and retrieve the output parameter.

This scenario is very simple but it is just for demonstration purposes and you can tweak it to your own needs.

Code of ‘mainWorkflow’:

Screen Shot 2015-11-04 at 16.50.47

//bind variables
var workflowPath = 'blog/'
var workflowNameToInvoke = 'invokedWorkflow'
//create execution properties
var executionProperties = new Properties()
executionProperties.put('stringInput',stringInput)
executionProperties.put('vmInput',vmInput)
//find worfklow catagory and get all workflows
var workflows = Server.getWorkflowCategoryWithPath(workflowPath).allWorkflows
//loop over each workflow and find the workflow based on name
for each(var workflowObject in workflows){
	if(workflowNameToInvoke == workflowObject.name){
		//execute workflow and get workflowToken
		var currentToken = workflowObject.execute(executionProperties)
		//wait for workflow completions
		var complete = false;
		while(complete == false){
			if(currentToken != null && (currentToken.state != "running" && currentToken.state != "waiting")){
				System.log("Workflow '"+currentToken.name+"' terminated with status '"+currentToken.state+"'");
				complete = true;
			}
		}
		//get output from the workflow token
		var outputPropertiesToken = currentToken.getOutputParameters()
		System.log('outputPropertiesToken.keys: ' + outputPropertiesToken.keys)
		System.log("outputPropertiesToken.get('stringOutput'): " + outputPropertiesToken.get('stringOutput'))
		break;
	}
}

The code requires the path to a folder containing the workflow and the name of the workflow itself that needs to be invoked. Using the Server.getWorkflowCategoryWithPath(workflowPath).allWorkflows statement, we get all workflows that are located in the folder. Using a loop, we iterate over the workflow array and try to find the workflow that matches the value of the ‘workflowNameToInvoke’ variable.

An alternative to the method described above would be to pass the workflow object directly via input parameters or declare it via workflow attributes.

A workflow has, in most cases, input parameters. To execute a workflow with input parameters via javascript it is required to create a properties object (object consisting of key-value pairs, in the code above it is called ‘executionProperties’) and to populate it. The name of the keys need to be identical to the input parameters of the workflow that will be invoked. In this example:

  • stringInput
  • vmInput

When the property object is created, it can be passed to the execute() method that is available on each workflow object. Calling this method will execute the workflow (what a surprise!). The method returns a ‘WorkflowToken’ object. This object can be used to verify the state of the workflow (running, waiting,…) and to pause the main workflow until the invoked workflow is completed (workflow is called synchronously in this case).

The output parameters can be retrieved by invoking the getOutputParameters() method. This returns a properties object where the keys of the properties object correspond to the names of the output parameters of the invoked workflow. In this case it is a single key called ‘stringOutput’

Output of the system logs for the ‘invokedWorkflow’:

[2015-11-04 22:14:46.731] [I] Following variables were passed to the workflow: stringInput=This is the inputString, vmInput (name)=win

Output of the system logs for the ‘mainWorkflow’:

Note: the system logs of the ‘invokedWorkflow’ are not included in the ‘mainWorkflow’ system logs

[2015-11-04 22:14:46.751] [I] Workflow ‘invokedWorkflow’ terminated with status ‘completed’

[2015-11-04 22:14:46.754] [I] outputPropertiesToken.keys: stringOutput

[2015-11-04 22:14:46.754] [I] outputPropertiesToken.get(‘stringOutput’): Following variables were passed to the workflow: stringInput=This is the inputString, vmInput (name)=win

yannickstruyf