Integration Mapper Groovy Expressions
Practical examples for Pricefx Integration Manager
Purpose. This guide shows how to use inline Groovy expressions in Integration Manager mappers for conditional logic, calculations, string manipulation, null handling, and access to Camel exchange data.
Mapper context: Groovy expressions can access the current record through body, the Camel exchange through exchange, message headers through exchange.in.headers, and exchange properties through exchange.properties.
Quick reference
|
Object |
Purpose |
Example |
|
body |
Current source row as a map |
body['sku'] |
|
exchange |
Camel Exchange object |
exchange.properties['batchId'] |
|
exchange.in.headers |
Message headers |
exchange.in.headers['region'] |
|
exchange.properties |
Exchange properties |
exchange.properties['sourceSystem'] |
1. Uppercase and trim text
The safe-navigation operator (?.) avoids a NullPointerException when the source value is null.
<mappers>
<loadMapper id="product.mapper">
<groovy
expression="body.name?.trim()?.toUpperCase()"
out="label"/>
<groovy
expression="body.description?.trim()?.replaceAll('\\s+', ' ')"
out="attribute1"/>
</loadMapper>
</mappers>
2. Concatenate multiple fields
The Elvis operator (?:) supplies an empty string when a source field is null.
<groovy
expression="(body.firstName ?: '') + ' ' + (body.lastName ?: '')"
out="label"/>
3. Conditional mapping
Ternary expressions are useful for simple conditional mappings.
<groovy
expression="body.status == 'ACTIVE' ? body.price : 0"
out="attribute1"/>
<groovy
expression="body.quantity?.toInteger() > 100 ? 'BULK' : 'STANDARD'"
out="attribute2"/>
4. Convert and calculate numeric values
Source values are commonly strings, so explicitly convert them before arithmetic.
<groovy
expression="body.price.toBigDecimal() * body.quantity.toInteger()"
out="attribute1"/>
<groovy
expression="body.price.toBigDecimal() * 1.21"
out="attribute2"/>
5. Null-safe defaults
Use ?. for safe method calls and ?: for fallback values.
<groovy
expression="body.region ?: 'UNKNOWN'"
out="attribute1"/>
<groovy
expression="body.optionalField?.trim() ?: 'N/A'"
out="attribute2"/>
6. Extract part of a string
For simple left/right extraction, built-in converters may be easier to maintain:
<groovy
expression="body.code?.substring(0, Math.min(body.code?.length() ?: 0, 10))"
out="attribute1"/>
<body
in="code"
out="attribute1"
converterExpression="stringLeft(10)"/>
// Convert 32.5% to a calculable value :
<groovy expression="body.percent?.replace('%', '')?.trim()?.toBigDecimal()?.divide(100)" out="attribute1"/>
7. Reformat a date
Use Groovy when the date transformation is more complex than a standard converter.
<groovy
expression="java.time.LocalDate.parse(body.dateStr, java.time.format.DateTimeFormatter.ofPattern('dd/MM/yyyy')).format(java.time.format.DateTimeFormatter.ofPattern('yyyy-MM-dd'))"
out="attribute1"/>
<body
in="dateStr"
out="attribute1"
converterExpression="stringToDate"/>
8. Access an exchange property or header
This is useful when route-level metadata must be included in each mapped record.
<groovy
expression="exchange.properties['batchId']"
out="attribute1"/>
<groovy
expression="body.sku + '-' + exchange.in.headers['region']"
out="attribute2"/>
9. Complete mapper example
The following example combines constants, direct mappings, Groovy transformations, arithmetic, conditional logic, and a built-in converter.
<mappers>
<loadMapper id="import-products.mapper">
<constant expression="Products" out="name"/>
<body in="sku" out="sku"/>
<groovy
expression="body.name?.trim()?.toUpperCase()"
out="label"/>
<groovy
expression="body.price ? body.price.toBigDecimal() * 1.21 : 0"
out="attribute1"/>
<groovy
expression="body.quantity?.toInteger() > 100 ? 'BULK' : 'STANDARD'"
out="attribute2"/>
<groovy
expression="(body.firstName ?: '') + ' ' + (body.lastName ?: '')"
out="attribute3"/>
<body
in="active"
out="attribute4"
converterExpression="stringToBoolean"/>
</loadMapper>
</mappers>
Built-in converter guidance
Use converterExpression for built-in type converters. Common examples include:
· stringToDecimal
· stringToInteger
· stringToLong
· stringToDate
· stringToDateTime
· stringToBoolean
· stringLeft(n)
· stringRight(n)
<body
in="price"
out="attribute1"
converterExpression="stringToDecimal"
defaultValue="0"/>
Note: Converters cannot generally be chained on a single mapper field. Use one Groovy expression for multi-step transformations, or move complex logic into a separate processor or script.
XML and sandbox considerations
Because the Groovy expression is stored inside an XML attribute, XML-special characters must be escaped:
<!-- Use > instead of > inside an XML attribute -->
<groovy
expression="body.quantity?.toInteger() > 100 ? 'BULK' : 'STANDARD'"
out="attribute1"/>
· Use safe navigation (?.) or explicit null checks for optional source fields.
· Convert string values explicitly with toInteger(), toBigDecimal(), or toDouble() before arithmetic.
· Use only classes and methods permitted by the Groovy sandbox.
· Avoid reflection, anonymous classes, and non-whitelisted APIs.
· Keep inline expressions readable; move very complex logic into a separate Groovy processor or script.
Sources
· Groovy Expressions in Mappers: Groovy Expressions in Mappers
· Mappers: Mappers
· Converters: Converters
· Type Converters in Mappers: Type Converters in Mappers