Backend Scripting with SpEL
SpEL (Spring Expression Language) is the scripting engine used in tSM to power backend automation, workflow execution, and process logic. It allows you to reference entities, calculate values, enforce conditions, and interact with data programmatically — directly from inside BPMN processes, service tasks, SLA rules, and API hooks.
SpEL scripts run on the server, making them ideal for use cases where security, consistency, or background execution is required.
Where is SpEL Used?
Area | Typical Usage Examples |
---|---|
Process Engine (BPMN) | Conditions, timers, expressions in gateways/tasks |
SLA / KPI Rules | Dynamic target times, escalation logic |
API Hooks | Pre/post-processing of requests and payloads |
Custom Script Tasks | Inline backend logic inside automation workflows |
Event Triggers | Run code on entity lifecycle events (e.g., ticket created) |
SpEL Syntax Basics
- Similar to Java/Kotlin-style expressions
- Supports logic, math, object navigation, and custom helpers
- References start with
#
, e.g.#ticket
,#orderTotal
,#currentUser()
#if(#ticket.priority == 'High').then(#escalate = true).else(#escalate = false)
Common Variables
Variable | Description |
---|---|
#ticket, #order | The current entity context |
#currentUser() | Active user executing the logic |
#now() | Current timestamp |
#addDays(date, 2) | Utility to manipulate time values |
Gateway Condition Example
#order.totalAmount > 1000
Used in an exclusive gateway to route to "Manager Approval" if total exceeds 1000.
SLA Escalation Example
#now().plusHours(48)
Used to set a dynamic SLA deadline 48 hours from now.
API Hook Transformation
In an API pre-processing script:
#requestBody.customerName = #requestBody.customerName.toUpperCase()
Ensures incoming names are uppercase before saving.
Process Task – Script with Variable Set
#if(#ticket.category == 'Urgent') {
#ticket.priority = 'High'
}
Used in a script task to auto-update priority based on category.
Helpers and Utilities
Category | Functions & Helpers |
---|---|
Time | #now(), #addDays(), #diffInMinutes() |
Math | #sum(), #round(), #abs() |
Logic | #if(condition).then(...).else(...) |
Collections | #list(...), #contains(...) |
JSON/Map | #get(), #set(), #toJson() |
Over 1500 methods are available — check the built-in script editor in tSM for autocompletion and docs.
Debugging SpEL
- Open an entity (e.g., order, ticket)
- Click
⋯ → SpEL Console
- Run SpEL scripts in live context with suggestions
- View outputs, test logic, and iterate safely
Best Practices
- Always validate expressions in the SpEL Console
- Use
#if(...).then(...).else(...)
for clean conditional logic - Avoid side effects in conditions — keep logic pure
- Reuse helper functions to avoid duplication
- Document complex logic in comments where supported
JEXL vs. SpEL – Quick Comparison
Feature | JEXL (Frontend) | SpEL (Backend) |
---|---|---|
Execution Scope | In browser, at runtime | On server, in processes |
Access to $context | Yes | No – uses # variables |
Use Cases | UI logic, forms | Workflow, automation, APIs |
Multi-line Support | ❌ (single expression) | ✅ |
Debugging | JEXL Console (form-based) | SpEL Console (entity-based) |