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:

public class TeneoTextUtils {
    /**
    * Takes a String input, and returns it with the the first letter of 
    * each word capitalized and all other letters lower case
    */
    public static String capitalizeFirstLetters(String input) {
        input.toLowerCase().tokenize().collect { it.capitalize() }.join(' ')
    }

}

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 in the Resource file manager in order to make it available in the solution.

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

public class TeneoTextUtils {
    /**
    * Takes a String input, and returns it with the the first letter of 
    * each word capitalized and all other letters lower case
    */
    public static String capitalizeFirstLetters(String input) {
        input.toLowerCase().tokenize().collect { it.capitalize() }.join(' ')
    }

}

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. Click on the 'Solutions' tab in the top left
  2. Select 'Globals' on the left.
  3. Select the 'Scripts' tab.
  4. Select 'Solution Loaded'.
  5. Click on the 'Edit' button in the top right
  6. Paste the following groovy class into the editing window:

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

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

Was this page helpful?