Monday, October 23, 2017

How to use classes in vRealize Orchestrator

JavaScript allows you to create classes and use them in your workflow’s scriptable tasks. This allows you to create your own objects with functions and attributes which can be consumed in multiple workflows. Or to abstract the logic in functions which can easily be updated in case there are new functions or logical changes after updates. A typical use case would be a logger class which allows to modify the logging messages to follow a specific format or to log to specific targets using the same functionality everywhere.

So how to create a class in vRealize Orchestrator?

First you will need to create a new vRealize Orchestrator action, here in my example a helloWorldClass action:


Set the output of the class to Any and insert the code you want to define your class. At the end output your class so it gets returned out of the action. In my example my code looks like follows:
var helloWorldClass = {
    hello: "Hello",
    world: "World",
            get: function () {
                        return this.hello + ' ' + this.world;
            },
    log: function () {
        System.log(this.hello + ' ' + this.world);
    }
}
return helloWorldClass;


Now you can consume your class from any workflow you want. Just create a new variable and point it to your action. In my example, it would look like this:
var helloWorldClass = System.getModule("sandbox").helloWorldClass();

Using the new variable helloWorldClass you have access to the attributes and functions of your declared class. In my example, I’m using the get() and the log() function to log Hello World once using System.warn() and once with the function inside the class:
// Hello World Class Test
var helloWorldClass = System.getModule("sandbox").helloWorldClass();
var helloWorld = helloWorldClass.get();
System.warn(helloWorld);
helloWorldClass.log();


The output looks like follows:



So you can see, it’s very easy to use your own classes in vRealize Orchestrator.