Groovy basics

Scripts in Teneo are written in Groovy or Java code. Apache Groovy is similar to Java but with an easier to learn syntax. More details on Groovy can be found here: An overview of the differences between Groovy and Java can be found here: Differences with Java.

This page will provide a quick overview of Groovy snippets and constructs that can come in handy in scripts in Teneo.

To print something to the Engine Ouput panel in tryout (and the console.log) use:

println "Hello world!"

As you can see you don't need semi-colons to end statements in Groovy.


In Groovy you can use dynamic typing. You use the keyword def to define a variable.

def x = true
println x.getClass()

x = 42
println x.getClass()

x = "Hello World"
println x.getClass()


class java.lang.Boolean
class java.lang.Integer
class java.lang.String


When using single quotes, you will use a standard Java String:

def name = 'Dave'
println 'Hello, ' + name + '. You\'re looking well today.'

When using double quotes, you can use interpolation:

def name = "Dave"
println "Hello, ${name}. You're looking well today."


def text = "Hunky dory!"
def name = text[0..4]
println name



Other example:

def text = "Hunky dory!"
def name = text[-5..-2]
println name



The Groovy Truth

Groovy evaluates every object to a boolean value if required (also know as the Implicit Truth):

if ("hunky dory") ...
if (42) ...
if (someObject) ...
  • Strings: If empty false, otherwise true
  • Collections and Maps: true if they are not empty
  • Numbers: true if non-zero
  • Object references: true if they aren't null

For more details: Groovy Truth


Safe Navigation Operator

The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. In Java it would look something like this:

if (order.getContact() != null && order.getContact().getAddress() != null) { 

In Groovy:

println  order?.getContact()?.getAddress()

Elvis Operator

The "Elvis operator" is a shortening of the ternary operator, often used to assign default values. In Java you would use them like this:

displayName = ? : 'Anonymous' 

Using the Elvis operator in Groovy:

displayName = ?: 'Anonymous'

(Turn your head sideways and look at the smiley's hair and you know why it is called the Elvis operator.)

More on operators in the Groovy docs: Operators



Creating a list:

def list = [1,2,2,4,5]

Accessing elements in the list:

println list[0] // will print out 1 
println list[-1] // use negative indexes for access from the end of the list, will print 5

Iterating lists:

list.each {
    println it


list.each { myNumber ->
    println myNumber

With index:

list.eachWithIndex { myNumber,  index ->
    println "$index, $myNumber"

Will print:

0, 1
1, 2
2, 2
3, 4
4, 5

Quick way of adding something to a list:

list << 6 // list will become [1,2,2,4,5,6]


Creating a map:

def map = ['key1':'value 1', 'key2':'value 2', 'key3':'value 3']

Other options:

def key = 'key3'
def map = [
  'key1': 'value 1',
  key2: 'value 2', // skip the quotes, the key will automatically be a String
  (key): 'value 3' // put the key in parentheses if you want to use the value of a variable

Accessing the map:

println map['key1']
println map.key1
println map[key] // access the entry with the value of key variable
println map.get(key) // using the get method with a key variable

Iterating maps:

map.each {
  println it.key
  println it.value


map.each { key, value ->
  println key
  println value

Adding something to a map:

map << ['key4':'value 4']

For more information on lists and maps in Groovy: Working with collections


Groovy's handling of JSON is very powerful.

Parsing JSON

def jsonSlurper = new groovy.json.JsonSlurper()
def object = jsonSlurper.parseText('{"name": "Dave Bowman",  "age": 32 }')

Producing JSON

Using JsonOuput:

def json = new groovy.json.JsonOutput().toJson([name: 'Dave Bowman', age: 32])

// indent nicely
def pretty = new groovy.json.JsonOutput().prettyPrint(json)

Using JsonBuilder:

def actionBuilder = new groovy.json.JsonBuilder()

actionBuilder {
    name "displayCard"
    parameters {
        type 'basic'
        title 'Cappuccino'
        image 'https://some.url/for/image/cappuccino.png'
        description 'Try our cappuccino! We love it for its remarkable body of fruit and mocha and its subtle sweet finish.'
def json = actionBuilder.toString()

// indent nicely
def pretty = groovy.json.JsonOutput.prettyPrint(json)

println pretty


    "name": "displayCard",
    "parameters": {
        "type": "basic",
        "title": "Cappuccino",
        "image": "https://some.url/for/image/cappuccino.png",
        "description": "Try our cappuccino! We love it for its remarkable body of fruit and mocha and its subtle sweet finish."

More on Groovy's way of handling JSON here: Parsing and producing JSON

Reading URL content

Getting a URL

def baseUrl = 'https://some/url/'
def text = baseUrl.toURL().text

Getting a URL and parsing the JSON response

def baseUrl = 'https://some/url/'
def result = new groovy.json.JsonSlurper().parseText(baseUrl.toURL().text)

Setting a time out on requests

def baseUrl = 'https://some/url/'
def result = new groovy.json.JsonSlurper().parseText(baseUrl.toURL().getText([connectTimeout: 2000, readTimeout: 2000]))

If it takes a long time for Teneo to respond and you see an error like this in Try Out: Script action execution failed: Maximum execution time exceeded, but forced script termination failed, the cause is often a call to an online API that is slow or doesn't respond. Setting a timeout will prevent this error. However, you will still need to make sure that your script or flow gracefully handles the fact that it did not receive a response in time.

Was this page helpful?