Code snippet : Find a workflow by name

vCenter Orchestrator references workflows by ID and allows to have multiple workflows with the same name. If the workflows you are calling have a unique name you can write a vCO wrapper to find a workflow by name with the following code.

Input: wfName (string)

Output: wfByName (Workflow)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var workflows = Server.findAllForType("Workflow", "c.name='"+wfName+"'");
var wfByName = null;
if (workflows != null){
  if (workflows.length == 1){
    System.log("Match found for workflow named: "+wfName);
    wfByName = workflows[0];
  }else{
    System.log("More than one VM found with that name! "+wfName);
    for each (wfl in workflows){
      System.log("Workflow ID: "+wfByName.id);
    }
  }
}

The interesting part is the second parameter of the findAllForType method : it is a query running on the server side, hence making the find a lot quicker than getting all the workflows and iterating through them to get the ones with the matching name.