Skip to content

Writing and reusing actions

Recipe 3.2 wrote a first action and called it from a workflow. Once you have a few dozen workflows, actions become the real library of your Orchestrator: the place where “get the facts about a VM” or “turn a size into a readable string” is written once and used everywhere. That only works if the actions are small, predictable, and safe to change.

In this recipe you build three actions in com.vcfguide.chapter4. getVmFacts collects a VM’s details into a Properties object. formatVmFacts turns those facts into one line of text. describeVm calls the other two. Then you change getVmFacts the safe way, look at its version history, and check who depends on it.

Getting ready

  • The action module com.vcfguide.chapter4 from recipe 4.1.
  • The vCenter registered in recipe 1.9, vc01.vmw.lab, with at least one VM. The actions only read from it.

How to do it

An action that gathers facts

  1. In Library > Actions, create the action getVmFacts in the module com.vcfguide.chapter4.
  2. On the General tab, set the Version to 1.0.0 and add the description Returns the name, power state, size, and guest OS of a VM as Properties.
  3. On the Script tab, add one input, vm, of type VC:VirtualMachine, and set the Return type to Properties. Enter this script and click Save:
com.vcfguide.chapter4/getVmFacts
// Collects the facts most workflows need about one VM. Reads only.
if (!vm) {
throw "getVmFacts: the input vm is required";
}
var facts = new Properties();
facts.put("name", vm.name);
facts.put("powerState", vm.runtime.powerState.value);
facts.put("cpus", vm.summary.config.numCpu);
facts.put("memoryMb", vm.summary.config.memorySizeMB);
facts.put("guestOs", vm.summary.config.guestFullName);
return facts;
  1. Click Run. Click in the vm field to open the Select VC:VirtualMachine dialog, open vSphere vCenter Server > your vCenter > Datacenters > your datacenter > vm down to the folder that holds the VM, tick the VM, and click Select. Click Run. The Last Run Details panel opens under the editor: on Result / Inputs, expand Action Result to see the five keys and their values, and Logs shows anything the action wrote. Drag the bar above the panel upward if the rows are cut off.
Action getVmFacts after a completed run. The Result / Inputs tab shows the input vm and the expanded Action Result: memoryMb 2048, guestOs Rocky Linux (64-bit), powerState poweredOn, cpus 1, name rockylinux9.
Figure 4.3. A test run of getVmFacts, with its Properties result.

An action that formats them

  1. Create the action formatVmFacts in the same module, with one input facts of type Properties and the return type string:
com.vcfguide.chapter4/formatVmFacts
// Turns the Properties from getVmFacts into one readable line.
if (!facts || !facts.get("name")) {
throw "formatVmFacts: facts must come from getVmFacts";
}
return facts.get("name") + ": " + facts.get("powerState") + ", " +
facts.get("cpus") + " vCPU, " + facts.get("memoryMb") + " MB, " +
facts.get("guestOs");

An action that calls the other two

  1. Create the action describeVm, with one input vm of type VC:VirtualMachine and the return type string:
com.vcfguide.chapter4/describeVm
var chapter4 = System.getModule("com.vcfguide.chapter4");
var facts = chapter4.getVmFacts(vm);
var line = chapter4.formatVmFacts(facts);
System.log(line);
return line;
  1. Run describeVm with a VM. The log and the result show one line, built by the two actions it called.

Change an action without breaking its callers

  1. Open getVmFacts again. Add a sixth fact after the guestOs line:
com.vcfguide.chapter4/getVmFacts — added line
facts.put("host", vm.runtime.host ? vm.runtime.host.name : "");
  1. On the General tab, change the Version to 1.1.0 and click Save. Callers that only read the first five keys, such as formatVmFacts, carry on unchanged.
  2. Open the Version History tab. A timeline on the left lists each saved version with its date, a short commit ID and the user who saved it, newest first, and a Restore link under every version except the current one. On the right, Selected Version is the current version and Diff against picks the one to compare it with. Choose 1.0.0 to see the added line.
Figure 4.4. Comparing two versions of an action.
Needs a lab check. On 9.1.1, the General tab shows Version as three number boxes. Still to check: whether saving without changing the number bumps it automatically, and whether Restore saves the old content as a new version. In the lab, an action whose versions were all saved through the REST API has an empty Version History timeline, and both sides of the diff show // Nothing to show. (checked on 9.1.1, 1 Dec 2026), so Figure 4.4 needs versions saved in the client.

Find out who uses an action

  1. Before you change an action’s inputs, rename it, or delete it, check what calls it. Open the action and click Find usages at the top of the editor.
Action editor for findVmsByName in module com.vcfguide.chapter3, with the dialog Usages of findVmsByName: Find a VM by name, Workflow, Scripting: item1, line 3; List VMs by name, Workflow, Workflow Schema: item1. 1 - 2 of 2.
Figure 4.5. Find usages of findVmsByName: one call from a script, one action element on a schema.

The list covers both ways of calling an action. An action element on a schema shows as Workflow Schema : item1. A call from a script, written as System.getModule("com.vcfguide.chapter3").findVmsByName(...), shows as Scripting : item1, line 3, with the line number. Find dependencies, beside it, answers the opposite question: which actions this action calls. The REST API has the same lookup as GET /actions/{id}/dependencies.

How it works

An action is a JavaScript function with a name, typed inputs, and one typed return value, saved once in Orchestrator’s database. Its full name is its module plus its name, com.vcfguide.chapter4/getVmFacts. The module is only a namespace, written like a Java package, which keeps your actions apart from the hundreds that ship with Orchestrator and its plug-ins under com.vmware.library.

System.getModule("com.vcfguide.chapter4") returns an object whose methods are the module’s actions. Calling chapter4.getVmFacts(vm) runs that action straight away, in the same thread as the script that called it, and hands back its return value. The action can’t see the caller’s variables. It only sees its own inputs, which keeps it predictable.

Arguments are passed by position, in the order the inputs are declared on the action’s Script tab. The names don’t matter to the caller. That’s the one thing that makes changing an action risky:

  • Adding a key to a returned Properties, as in step 8, is safe. Callers ignore keys they don’t read.
  • Adding a new input at the end is fairly safe. Old callers pass nothing for it, so it arrives as null or undefined, and the action can fall back to a default.
  • Adding an input in the middle, reordering inputs, or changing a type silently breaks every caller that uses the old order.
  • Renaming an action or moving it to another module breaks every script that calls it with System.getModule, and can break action elements too. Check its references first, as in step 11.

The Version field is a label for people. Orchestrator doesn’t keep several callable versions side by side: every caller always gets the latest saved script. Raising the version tells the next reader, and the next package import (Chapter 6), that something changed. The Version History tab keeps the old scripts so you can see what changed and go back.

Back in vRO 5.5–7Create actions in the Java client's Actions view, with inputs and the return type on the Scripting tab, and raise the version number by hand. Compare versions only after exporting them.
In Orchestrator 9.xCreate actions under Library > Actions in the browser. Every save is kept on the Version History tab, ready to compare or restore, and an action can be written in Python, Node.js, or PowerShell as well as JavaScript.

There's more

One job per action. Name actions with a verb and an object: getVmFacts, findVmsByName, formatVmFacts. If you can’t name an action without “and”, it’s two actions. Small actions are easier to test with Run, easier to reuse, and easier to fix.

Check inputs and fail loudly. A clear throw at the top of an action, as in steps 3 and 5, turns a confusing failure deep inside a workflow into a one-line message that says what was wrong. The caller’s exception path (recipe 3.4) receives that message.

Return Orchestrator types. Return a type that can be bound and stored with a run: a plug-in type, a string, a number, an Array/…, or Properties. The return type Any accepts anything, but it moves type checking from save time to run time, where it costs more. Keep it for actions that really return different things.

Don’t edit library actions. Actions under com.vmware.library belong to Orchestrator and its plug-ins, and an upgrade can replace them. If one is nearly right, copy its script into your own module and change the copy.

Actions in forms. Recipe 3.6 used actions to fill drop-downs and validate fields. Those run every time the form changes, so keep them fast and free of side effects. An action that writes something has no place behind a form.

Needs a lab check. Check whether 9.1.1 has the action cache described in Broadcom KB 421999 for 8.18.1, where a workflow run within about two minutes of saving an action could still use the old script.

Deleting an action. Check its references first. Through the REST API, DELETE /actions/{id} refuses an action that’s still referenced unless you add force=true, which leaves those workflows broken. Fix the callers first.

Moving actions between Orchestrators. You can export a single action to a file and import it elsewhere, but it’s easy to forget the actions it calls. A package (Chapter 6) carries an action together with everything it depends on.

See also