Formula - Sum
Section titled “ Formula - Sum”The sum function, one of the Math functions in the Formula plugin for Search Field Expressions, returns the total of the numeric values in the content.

Animated walkthrough: on a three-line Order Entry XML of the Order-to-Cash demo, count of xPath('OrderEntry/OrderLines/OrderLine/LineId', body) returns 3. Then sum(xPath('OrderEntry/OrderLines/OrderLine/LineTotal', body)) adds up 399.80, 498.00 and 125.00, and returns 1022.80.
Syntax
Section titled “ Syntax”sum(content [, unique])Parameters
Section titled “Parameters”| Parameter | Required | Description |
|---|---|---|
content |
✅ Yes | The numeric values to add up: body, context, or another Formula function such as jsonPath. |
unique |
❌ No | Unquoted true or false; default false for this function. false: a value that appears several times counts every time. true: identical values count once. Values are compared as text, so 100 and 100.00 are different values. A quoted 'true' is an error. |
Returns
Section titled “Returns”- One number: the total of the numeric values in
content. - A single value is returned as it is (see Example 2).
Quoting, nesting, and how content is resolved are described in the Formula language rules.
Examples
Section titled “ Examples”Example 1: Total of the invoice line amounts in JSON
Section titled “Example 1: Total of the invoice line amounts in JSON”Message Body:
{"invoice":{"lines":[{"amount":120},{"amount":80}]}}Formula
Section titled “Formula”sum(jsonPath('$..amount', body))Result
Section titled “Result”200jsonPath extracts the line amounts 120 and 80; their total is 200.

$..amount finds amount at any depth, so every invoice line is included in the total.
Example 2: Sum the message body
Section titled “Example 2: Sum the message body”Message Body:
2Formula
Section titled “Formula”sum(body)Result
Section titled “Result”2
The message body holds one value, 2, so the total is 2.
Example 3: Sum a message context value
Section titled “Example 3: Sum a message context value”Message Context:
| Key | Value |
|---|---|
id |
4 |
Formula
Section titled “Formula”sum(context('id'))Result
Section titled “Result”4
The context key id holds one value, 4, so the total is 4.
Example 4: Total of values extracted from JSON
Section titled “Example 4: Total of values extracted from JSON”Message Body:
{a:[{b:'1'},{b:'2'},{b:'3'}]}Formula
Section titled “Formula”sum(jsonPath('a[*].b', body))Result
Section titled “Result”6
jsonPath extracts 1, 2, and 3; their total is 6.
- For totals, leave
uniqueoff: two invoice lines of100give200. Withsum(..., true)they give100, because identical values count once—a wrong total when two lines happen to have the same amount. - Values that aren't numbers are skipped, without an error.
- The total keeps the decimals as written:
399.80 + 498.00 + 125.00returns1022.80, not1022.8. - Point xPath at the element that holds the number (
.../OrderLine/LineTotal). An element with child elements (.../OrderLine) returns no value.