Skip to content
Documentation
Updated 3 minutes to read
Markdown

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.

Count the order lines of an Order Entry XML, then add up their line totals with sum, animated walkthrough (7.12)
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.

sum(content [, unique])
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.
  • 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.

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}]}}
sum(jsonPath('$..amount', body))
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.

Message Body:

2
sum(body)
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.

Message Context:

Key Value
id 4
sum(context('id'))
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

Section titled “Example 4: Total of values extracted from JSON”

Message Body:

{a:[{b:'1'},{b:'2'},{b:'3'}]}
sum(jsonPath('a[*].b', body))
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.

  • 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.
  • Values that aren't numbers are skipped, without an error.
  • The total keeps the decimals as written: 399.80 + 498.00 + 125.00 returns 1022.80, not 1022.8.
  • Point xPath at the element that holds the number (.../OrderLine/LineTotal). An element with child elements (.../OrderLine) returns no value.

...

Version:
Release date: N/A
Loading...

Explore Nodinite for 30 days – completely free

  • Find out why this is the leading O&M software
  • Prove your business case
  • Test the self-service for the business side

Loading...

See how Nodinite resolves your technical issues, in a LIVE Demo!

Once you submit this request:

  • We will give you a short call to understand the issues you want to tackle.
  • Understand solutions you have tried and why were you not happy with them.
  • Book a convenient slot for a LIVE demo!

Loading...