Hello,
is there a way to call a workflow in a scriptable task like you can with actions?
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.
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);
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
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.
works like a dream - thanks!
How is possible to get Output (log) from this dynamic workflow ?
Or output variables ?
Thank you
outputProperties = token.getOutputParameters();
System.log('keys: ' + outputProperties.keys);
System.log('out1: ' + outputProperties.get('out1');
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 !
var vmName = outputProperties.get('vmName');
System.log(vmName);