Groovy Scripting

For more complex algorithms, complex data mappings and transformations sometimes a scripting language is required. Also see for more details on this.

PIPEFORCE directly embeds the powerful Groovy scripting engine into Pipelines. This way Groovy scripts can be executed in a very clean and well performing way with direct access to headers, vars and body scopes.

See here for a detailed Groovy Language Documentation.

Running a Groovy Script

In order to embed a Groovy script into a pipeline, you can use the script.groovy command like this example shows:

pipeline: - script.groovy: | def result = 1 + 1 body = "The result is: " + result

As you can see, the result is directly written to the body.

Note: The script.groovy command by default doesn’t write the result to the body. In order to change the body value you must do one of:

  • Assign the value to the default variable body, for example: body = "Some value"

  • Set the output parameter to ${body}

Accessing Pipeline Scopes (default variables)

You can directly access all pipeline scopes like headers, vars, body and so on inside the Groovy script:

headers: myKey: "World" vars: data: {"text": "Hello "} pipeline: - script.groovy: | def hello = def world = headers.myKey body = hello + world + "!"

These default variables available read-write within each Groovy script are:

  • headers = The pipeline headers.

  • vars = The pipeline variables.

  • body = The current pipeline body.

  • request = The current HTTP request if this pipeline was called by a HTTP request.

  • response = The current HTTP response if this pipeline was called by a HTTP request.

  • context = The current pipeline context object.

See for more details about these variables.

In version 9.0.1 the default variables can be accessed only via args. prefix. For example This has changed in version 10 so you also can access default variables without this prefix: Bit accessing via args. prefix is still supported for backwards compatibility.

Working with JSON

Also complex data mappings reading and writing JSON objects and arrays can be done using the command script.groovy. See this example:

vars: arr1: [ {"type_": "receiver_address", "mentionText_": "ABC"}, {"type_": "invoice_number", "mentionText_": "EFG"}, {"type_": "invoice_date", "mentionText_": "28.12.2023"}, {"type_": "supplier_name", "mentionText_": "someName"} ] arr2: [ {"id": "due_date", "mapping": "invoice_date"}, {"id": "invoice_date", "mapping": "invoice_date"}, {"id": "supplier_name", "mapping": "supplier_name"} ] pipeline: - script.groovy: | args.body = [:] for (entry in args.vars.arr2) { def id = entry["id"] def mapping = entry["mapping"] for (obj in args.vars.arr1) { if (obj.type_ == mapping) { args.body[id] = obj.mentionText_ break // Exit the inner loop once a match is found } } }

Using the Script Util

Another option to use Groovy us by using the @script.groovy() util. For example:

The optional variable #this which is passed here gives read-access to the current pipeline scopes, using the args. prefix.

You can also pass custom args to this util method: