Teneo Developers

Add a Groovy class

Sometimes you may want to add a class to your solution, which you can then call in your integrations or scripts.

On this page, we will show you two ways to add a Groovy class to your solution:

  • Approach 1: upload the class as a .groovy file
  • Approach 2: add the class to the 'Solution loaded' script

Both ways have the same outcome: a class that you can use in the various scripts and integrations in your solution.

But first, let's define a basic Groovy class that we can use an an example.

Example Groovy class

We will use the following class as an example to add to Teneo:

groovy

1public class TeneoTextUtils {
2    /**
3    * Takes a String input, and returns it with the the first letter of 
4    * each word capitalized and all other letters lower case
5    */
6    public static String capitalizeFirstLetters(String input) {
7        input.toLowerCase().tokenize().collect { it.capitalize() }.join(' ')
8    }
9
10}
11

The class is called 'TeneoTextUtils' and its method 'capitalizeFirstLetters' will capitalize each word in a String. For example: TeneoTextUtils.capitalizeFirstLetters('new york') returns 'New York'.

Approach 1: Add a Groovy class via a .groovy file

One way of adding the class to the solution is to save it as a text file with the name of the class and the extension .groovy. This file is then uploaded to the File Resources to make it available in the solution.

First, save the following code in a text file called TeneoTextUtils.groovy:

groovy

1public class TeneoTextUtils {
2    /**
3    * Takes a String input, and returns it with the the first letter of 
4    * each word capitalized and all other letters lower case
5    */
6    public static String capitalizeFirstLetters(String input) {
7        input.toLowerCase().tokenize().collect { it.capitalize() }.join(' ')
8    }
9
10}
11

Next, add the file to your solution and set the path to /script_lib (If you need guidance on how to add the file to the solution, please check Add files to your solution).

Now the class 'TeneoTextUtils' can be used in your scripts and integrations.

Approach 2: Add a class to the global 'Solution loaded' script

The other way of adding a class is by adding it to the global script Solution loaded. This script is compiled and loaded each time a solution is loaded into memory, which happens when you publish your solution, for example.

To add it, proceed as follows:

  1. Locate the Globals tile on your Solution dashboard and click on the Add icon next to Scripts.
  2. Create an 'On solution loaded' script called Capitalize first letters.
  3. Paste the following groovy class into the editing window:

    groovy

    1public class TeneoTextUtils {
    2    /**
    3    * Takes a String input, and returns it with the the first letter of 
    4    * each word capitalized and all other letters lower case
    5    */
    6    public static String capitalizeFirstLetters(String input) {
    7        input.toLowerCase().tokenize().collect { it.capitalize() }.join(' ')
    8    }
    9
    10}
    11
  4. Hit 'Save', then close the editing window.

The class 'TeneoTextUtils' can now be used in your scripts and integrations.