- 2 minutes to read

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.

Syntax

sum(content [, unique])

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

  • 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

Example 1: Total of the invoice line amounts in JSON

Input

Message Body:

{"invoice":{"lines":[{"amount":120},{"amount":80}]}}

Formula

sum(jsonPath('$..amount', body))

Result

200

jsonPath extracts the line amounts 120 and 80; their total is 200.

Test Expression tab: sum(jsonPath('$..amount', body)) on an invoice with line amounts 120 and 80 returns 200
$..amount finds amount at any depth, so every invoice line is included in the total.

Example 2: Sum the message body

Input

Message Body:

2

Formula

sum(body)

Result

2

Test Expression with sum(body) on the message body 2, returning 2
The message body holds one value, 2, so the total is 2.

Example 3: Sum a message context value

Input

Message Context:

Key Value
id 4

Formula

sum(context('id'))

Result

4

Test Expression with sum(context('id')) on the context key id with value 4, returning 4
The context key id holds one value, 4, so the total is 4.

Example 4: Total of values extracted from JSON

Input

Message Body:

{a:[{b:'1'},{b:'2'},{b:'3'}]}

Formula

sum(jsonPath('a[*].b', body))

Result

6

Test Expression with sum of jsonpath over every b in the array a, on a JSON array, returning 6
jsonPath extracts 1, 2, and 3; their total is 6.

Notes

  • For totals, leave unique off: two invoice lines of 100 give 200. With sum(..., true) they give 100, because identical values count once—a wrong total when two lines happen to have the same amount.

Next Steps