Skip to main content
Version: 2.4

SpEL Examples

This page contains a few small examples distilled from the golden-set scripts. They are intentionally basic and focus on patterns that appear often in real tSM scripts:

  • load one entity and shape a stable result
  • run a bounded search and map the output
  • clean an input list and compare it with existing records

1 Load One Customer and Return a Summary

This example loads one customer, expands the primary contact, and returns only a few selected fields.

// 1) Load the customer by ID or key.
#customer = @crm.customer.get(#customerId, {expand: ['PRIMARY_CONTACT']})
?: #businessException(
'CUSTOMER_NOT_FOUND',
'Customer not found: ' + #customerId
)

// 2) Return a stable summary.
{
customerId: #customer.id,
customerKey: #customer.key,
customerName: #customer.name,
customerType: #customer.customerType,
primaryContactId: #customer.primaryPerson?.id,
primaryContactEmail: #customer.primaryPerson?.emails?.first()?.email
}

Why this is useful:

  • demonstrates @crm.customer.get(...)
  • shows expand usage for related data
  • uses ?: #businessException(...) for required lookups
  • uses safe navigation for optional contact fields

2 Search Customers by Name

This example validates the input, performs a deterministic search, and maps the result list to a smaller response.

// 1) Validate and normalize the input.
#searchName = #isNullOrEmpty(#customerName)
? #businessException('CUSTOMER_NAME_REQUIRED', 'Customer name is required.')
: #customerName.trim()

// 2) Search a bounded, deterministic result set.
#customers = @crm.customer.find(
{'name__contains': #searchName},
{sort: 'name,ASC', size: 10}
)

// 3) Return a lightweight stable output.
{
searchName: #searchName,
count: #customers.size(),
customers: #customers.![{
customerId: id,
customerKey: key,
customerName: name
}]
}

Why this is useful:

  • shows input validation with #isNullOrEmpty(...)
  • uses find(...) with explicit sorting and page size
  • demonstrates list projection with .![...]
  • keeps output shaping separate from the query itself

3 Find Missing Privilege Codes

This example cleans an input list, loads existing privileges, and returns only the codes that do not exist.

// 1) Remove null, empty, and duplicate values from the input list.
#cleanMyPrivs = #myPrivs.?[!#isNullOrEmpty(#this)].distinct()

// 2) Load existing privileges through the public client API.
#existingPrivs = (#cleanMyPrivs == []
? []
: @user.priv.find(
{code__in: #cleanMyPrivs},
{sort: 'code,ASC', size: #cleanMyPrivs.size(), cache: false}
)
)

// 3) Return codes that were requested but not found.
#existingPrivCodes = #existingPrivs.![code]
#missingPrivs = #cleanMyPrivs.?[!#existingPrivCodes.contains(#this)]
#missingPrivs

Why this is useful:

  • shows list filtering with .?[...]
  • demonstrates .distinct() on list input
  • uses a public client instead of a database query
  • returns the last expression directly as the script result