VMware Cloud Community
sean_gadson
Enthusiast
Enthusiast

Call A workflow In Scriptable Task

Hello,

is there a way to call a workflow in a scriptable task like you can with actions? 

9 Replies
qc4vmware
Virtuoso
Virtuoso

Yes, but you have to retrieve the workflow object somehow.  There are plenty of ways to do that from statically defining it or looking it up by name or uuid etc.   It has a .execute() method.  You'll need to pass it in a properties type set correctly for each input needed to run the workflow.

Reply
0 Kudos
barjinders
VMware Employee
VMware Employee

The whole process can be summarised as below:

1) Get the workflow ID  for the workflow you want to run. You can get it either in code or copy it from GUI ->General Tab-> ID.

2)Use an inbuilt library action to get the WF Object.

    var wfObject = System.getModule("com.vmware.library.workflow").getWorkflowById("<WF ID>");

3) Input Parameters are passed as properties:

    var workflowParameters = new Properties();

     workflowParameters.put("<InputParameter Name>", <value>);

4) Execute the workflow with properties as input parameters. 

var token = wfObject.execute(workflowParameters);

5) Wait for the workflow to complete. You can use an inbuilt library action for that.

            System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete([token]);

           

6) Log if you want:

System.debug("Workflow: " + token.name + "State: " + token.state);

ImranMughal01
Enthusiast
Enthusiast

how can you grab the workflow ID from its name in a scriptable task?

So i dynamically want to run a different workflow in the scriptable task on depending on what the script is doing.

For instance, my script loops through API calls defined in a config attribute and depending on the API call I need to run a different workflow each time.

I don't want to have to map a workflow id to a workflow name as a definition in a config attribute if possible.

thanks

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Here is some scripting code showing how to retrieve workflow(s) with a given name:

var all = Server.query("Workflow", "name='My custom workflow'"); // provide your workflow name in the second parameter

for each (var wf in all) {

  System.log(wf.id);

}

Note that there could be more than one workflow with the same name (in different folders), so you need to add some logic to handle this situation.

Another way to find the workflow(s) by name is to enumerate all workflows and filter out those whose names match. Check the scriptable action com.vmware.library.workflow/getAllWorkflows for sample code how to recursively enumerate all workflows.

ImranMughal01
Enthusiast
Enthusiast

works like a dream - thanks!

Reply
0 Kudos
sabom
Contributor
Contributor

How is possible to get Output (log) from this dynamic workflow ? 

Or output variables ?

 

Thank you

Reply
0 Kudos
xian_
Expert
Expert

outputProperties = token.getOutputParameters();
System.log('keys: ' + outputProperties.keys);
System.log('out1: ' + outputProperties.get('out1');
citizen95
Contributor
Contributor

Hello,

I'm struggling to get the content of the output.. 

The :

outputProperties = token.getOutputParameters();
System.log('keys: ' + outputProperties.keys);

is returning the name of the variable, which in my case is "vmName", it's an array of string on the original workflow. but no way I can get the content to perform some tests on it ..  

Any help will be much appreciated !

 

Reply
0 Kudos
xian_
Expert
Expert

var vmName = outputProperties.get('vmName');
System.log(vmName);