---
title: Formula sum – Total of Numeric Values in Nodinite Search Field Expressions
description: The sum function in the Nodinite Formula plugin returns the total of the numeric values in the content. Syntax, the unique parameter, and tested examples for message body, message context, and jsonPath.
---

# :fa-function: 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.

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

## :fa-code: Syntax

```text
sum(content [, unique])
```

### Parameters

| Parameter | Required | Description |
| --- | --- | --- |
| `content` | ✅ Yes | The numeric values to add up: [body][], [context][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](#example-2-sum-the-message-body)).

Quoting, nesting, and how `content` is resolved are described in the [Formula language rules][].

## :fa-list-ol: Examples

### Example 1: Total of the invoice line amounts in JSON

#### Input

Message Body:

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

#### Formula

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

#### Result

```text
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][ex1]  
*`$..amount` finds `amount` at any depth, so every invoice line is included in the total.*  

### Example 2: Sum the message body

#### Input

Message Body:

```text
2
```

#### Formula

```text
sum(body)
```

#### Result

```text
2
```

![Test Expression with sum(body) on the message body 2, returning 2][1]  
*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

```text
sum(context('id'))
```

#### Result

```text
4
```

![Test Expression with sum(context('id')) on the context key id with value 4, returning 4][6]  
*The context key `id` holds one value, `4`, so the total is `4`.*  

### Example 4: Total of values extracted from JSON

#### Input

Message Body:

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

#### Formula

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

#### Result

```text
6
```

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

## :fa-circle-info: 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.
* 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.

## :fa-hand-point-right: Next Steps

* :fa-magnifying-glass-plus: [Add or manage Search Field][]  
* :fa-flask: [Test Your Expression][]  
* :fa-hard-drive: [Add or manage Log View][]  

## :fa-link-horizontal: Related Topics

* :fa-calculator: [avg][average]  
* :fa-hashtag: [count][]  
* :fa-text-width: [length][]  
* :fa-arrow-up-wide-short: [max][]  
* :fa-arrow-down-short-wide: [min][]  
* :fa-circle-half-stroke: [round][]  
* :fa-function: [Formula][]  
* :fa-scroll: [Formula language rules][]  
* :fa-puzzle-piece: [Search Field Expressions][]  

[1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/sum_simpleBody.png
[2]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/Formula_SumCount.gif
[3]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/sum_jsonPath.png
[6]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/sum_contextMessage.png

[Add or manage Log View]:{ndr}/Administration/1.%20Log%20Management/1.%20Log%20Views/Features/Add%20or%20Manage%20Log%20View.md
[Add or manage Search Field]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md
[average]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/average.md
[body]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/body.md
[Context]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/context.md
[count]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/count.md
[ex1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/sum_Example1.png
[Formula]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md
[Formula language rules]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md#fa-scroll-formula-language-rules
[jsonPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/jsonPath.md
[length]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/length.md
[max]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/max.md
[min]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/min.md
[Order-to-Cash demo]:{ndr}/Repository%20Model/BPMs/Examples/Order-to-Cash/Generate-O2C-Demo-Data.md
[round]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/round.md
[Search Field Expressions]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Overview.md
[Test Your Expression]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md#test-your-expression
[xPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath.md