Script Body Guide (Groovy Java)

This page explains how to write the Script Body for an Alert Script. The script runs server-side and synchronously when the trigger event fires (Create / Update / Delete).

The script acts as a boolean filter:

  • Return true → the event is significant and an alert should be generated

  • Return false → suppress the alert for this event


1) Script Body rules (must-follow)

  • Groovy only

  • The Script Body is the inside of a method ✅ Write statements directly ❌ Do not add a class or method signature

  • Always return a boolean on every path

  • Prefer early return false for readability and safety


2) Runtime variables (available without declaration)

These are injected into the script context automatically:

Core context

  • dlpObject (Object) Current state of the object that triggered the event.

  • oldDlpObject (Object) Previous state of the object only for UPDATE events. Often null on INSERT, and may not match expected type.

  • wsUser (User) The user/system account that performed the action. Useful for role-based suppression.

Message output

  • mapResultsForMessage (Map<String, String>) Placeholder map for template-based message text. Typical keys are "?1", "?2" etc.

  • mapMessageFromServer (Map<String, String>) Full message override. Use key "MSG" to provide the message text.


A clean script usually follows this flow:

  1. Imports (only what you use)

  2. Type check + cast dlpObject (and optionally oldDlpObject)

  3. Guard clauses for nulls / wrong event shape

  4. Business logic (status checks, threshold checks, role checks, etc.)

  5. Populate message values (template placeholders or full message)

  6. return true


4) Type check + casting (avoid ClassCastException)

dlpObject and oldDlpObject are generic Object, so type-check first.

For old object (updates only):


5) Null-Safety: Patterns and Best Practices

Assume linked objects can be null (headers, vessel, statuses, etc.). Use:

  • Guard clauses

  • Groovy safe navigation ?.

  • Null coalescing ?:


6) Field access and status comparisons

Linked object navigation

Status fields

Compare status codes, not the status object:

circle-info

Tip: "LITERAL".equals(x) is null-safe and avoids surprises.


7) Update/change detection (current vs old)

If your trigger is UPDATE and you only want alerts on change:

Step-by-step pattern

Null-safe comparisons

Avoid calling .equals() on potentially null values.

Numeric thresholds (example)


8) Message building modes (CRITICAL – prevents misconfiguration)

There are two valid combinations between the Script Body and Message Configuration.

Mode A — Build message in script = true

Use this when the Script Body produces the full message text.Requirements

  • Build message in script = true

  • Script must set: mapMessageFromServer["MSG"]

Mode B — Build message in script = false

Use this when the server builds the message using a template string (messageTxt) and placeholder map.

Requirements

  • Build message in script = false

  • messageTxt must be provided (example: "Hello ?1")

  • Script sets: mapResultsForMessage["?1"] = "World"

Example configuration:

  • messageTxt = "Hello ?1"

Script:

The broken combo

Don’t do this:

  • mapResultsForMessage + Build message in script = true

Reason: Build message in script = true expects "MSG" in mapMessageFromServer.


When auto-generating alert script configs, set Build message in script using this rule:

  1. If Script Body contains mapMessageFromServer → set true

  2. Else if Script Body contains mapResultsForMessage AND messageTxt is set → set false

  3. Else → fail validation or require the missing pieces (recommended)

This prevents “alerts firing with empty messages” situations.


Sometimes the needed data is not reachable from dlpObject (link not loaded / not present). You can query the database using:

circle-exclamation

11) Troubleshooting

Logging

Print debug information to application logs:

Return-path sanity

If alerts don’t fire:


12) Full example (safe, readable, and template-based)

Config:

  • Build message in script = false

  • messageTxt = "Voyage ?1 PNL changed to ?2"

Script:

Last updated

Was this helpful?