Aria Automation Orchestrator Tips: JavaScript GOTO

Share on:

How to exit or jump in JavaScript code within a scriptable task

Introduction

Orchestrator actions behave like JavaScript functions: it is very easy to exit an action by using the keyword return. On the other hand, scriptable tasks written in JavaScript does not allow to use return to finish the execution of the current task. This can make the code unnecessary more complex and harder to read. In this post I'll demonstrate a possible syntax solution to jump out of the code immediately (similar to GOTO).

Sample action code: create a configuration element

For demonstration purposes we'll write a simple code to create a new configuration element in a given folder. Orchestrator has a built-in function to make a new element: Server.createConfigurationElement(category, name);
This function either creates an empty configuration element in the category (folder) with the given name, or throws an error if the element already exists. To avoid the error, we can get the configration elements in a folder: get the category object by Server.getConfigurationElementCategoryWithPath(category); and enumerate the elements in the category object attribute configurationElements.

Let's create an action called createConfig with inputs category and name that'll return true if a new configuration element was created or false if the element already present.

 1cat = Server.getConfigurationElementCategoryWithPath(category);
 2
 3for each(ce in cat.configurationElements) {
 4    if(ce.name == name) {
 5        message("Config element already existed: " + name);
 6        return false;
 7    } 
 8}
 9
10message("Config element did not exist, creating: " + name);
11Server.createConfigurationElement(category, name);
12return true;
13
14function message(msg) {
15    System.debug(msg);
16}

We can easily jump out of the code by returning false if we find a configuration element with the name requested. (We added a simple function message() for later use.)

Scriptable task (re)implementation

Let's see how we can rewrite the same code as scriptable task.

 1cat = Server.getConfigurationElementCategoryWithPath(category);
 2created = true;
 3
 4for each(ce in cat.configurationElements) {
 5    if(ce.name == name) {
 6        message("Config element already existed: " + name);
 7        created = false;
 8    } 
 9}
10
11if (created) {
12    message("Config element did not exist, creating: " + name);
13    Server.createConfigurationElement(category, name);
14    created = true;
15}
16
17function message(msg) {
18    System.debug(msg);
19}

As you can see, we used the output variable created to allow running the second half of the code, when we could not find the configuration element. The code is harder to read and follow.

JavaScript break to the rescue

JavaScript break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement - according to the language refefence. Let's create a labeled block and jump (GOTO) after the block. This will allow us to immediately exit from the current scriptable task, similar to the first implementation within an action:

 1main: {
 2    cat = Server.getConfigurationElementCategoryWithPath(category);
 3
 4    for each(ce in cat.configurationElements) {
 5        if(ce.name == name) {
 6            message("Config element already existed: " + name);
 7            created = false;
 8            break main;
 9        } 
10    }
11    
12    message("Config element did not exist, creating: " + name);
13    Server.createConfigurationElement(category, name);
14    created = true;
15}
16
17function message(msg) {
18    System.debug(msg);
19}

We do not have to use a (possibly) huge if(){} block here. If our code logic is more complex, we can save even more nested if's as we can jump out of the main labeled block at any time, just like in an action with the return statement.

It is important to note that function definitions must be done outside of the labeled block. In this simple example we exit the scriptable task as the block is the last (non-function definition) code, but we could write additional statements after the block. Also, we can use nested labeled blocks if necessary and control code execution even further.

Wrap-up

Although JavaScript does not have a GOTO statement, with the above syntax sugar you can make your code simpler. I hope you liked the idea and find it useful.