Code snippets : Cancel one of your running workflows

It has been a while without publishing a tutorial. With the growing activities around vCO the vCO team members have been very busy and doing such tutorials takes a lot of time. Since a lot of you already went through the basics we can now give you some code snippets that you can use in your own workflows. Here is the first one. I will eventually improve the article if people ask for it.

Using vCenter Orchestrator has some advantages. One of them is that it is easy to mange your workflows.

In this example we will get the active workflows for the current user and cancel the select one. First we need to create an action that returns the active workflows for the current user. Create an action named getCurrentUserActiveWorkflowTokens or similar and edit it.

Make sure to set the return type to Array of WorkflowToken. A workflowToken is the object created when you execute a workflow. It contains all kind of useful data on the worklow.

Here is the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  var currentUserName = Server.getCredential().username; 
workflowTokens = Server.findAllForType('WorkflowToken'); 
//Get all executions 
var myWorkflowTokens = new Array(); 
for each (var workflowToken in workflowTokens) {
  if (workflowToken.runningUserName == currentUserName && (workflowToken.state != "canceled" && workflowToken.state != "completed" && workflowToken.state != "failed")) {
    myWorkflowTokens.push(workflowToken);
    System.log(workflowToken.name + ":" + workflowToken.state);
  }
 } 
return myWorkflowTokens;

First we get the current user, than get all the workflow executions stored in the vCO server and then filter the ones that belong to the current user and in a non ended state.

Now create a new "Cancel my workflow" workflow with an input named activeWorkflow of type workflowToken. In the presentation tab add a "Predefined list of elements" property. Use the icon on the right to associate an action. Select the action you have just created.

In the workflow add a scriptable box, select the workflowToken input as its input parameter and write the code:

1
activeWorkflow.cancel();

Validate, add an end / link your box, save and try it. You can now cancel any of your running workflows. This may be useful in case running workflows may decomission VMs you still need !