Formula - Count
Section titled “ Formula - Count”The count function, one of the Math functions in the Formula plugin for Search Field Expressions, returns how many values the content holds.

Animated walkthrough: on a three-line Order Entry XML of the Order-to-Cash demo, count(xPath('OrderEntry/OrderLines/OrderLine/LineId', body)) returns 3. The same order then shows sum of the line totals: 1022.80.
Syntax
Section titled “ Syntax”count(content [, unique])Parameters
Section titled “Parameters”| Parameter | Required | Description |
|---|---|---|
content |
✅ Yes | The values to count: body, context, or another Formula function such as xPath or jsonPath. |
unique |
❌ No | Unquoted true or false; default true 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: how many values
contentholds. - Duplicates are counted once by default, because
uniquedefaults totrueforcount(see Example 5). Usecount(..., false)to count every occurrence.
Quoting, nesting, and how content is resolved are described in the Formula language rules.
Examples
Section titled “ Examples”Example 1: Count the orders in an XML message
Section titled “Example 1: Count the orders in an XML message”Message Body:
<ns0:Orders xmlns:ns0="SupplyChain.Schemas/1.0"> <Order> <Id>1337</Id> <Amount>666</Amount> <City>New York</City> <Partner>QWNtZQ==</Partner> </Order> <Order> <Id>1338</Id> <Amount>42</Amount> <City>Paris</City> <Partner>QWNtZQ==</Partner> </Order></ns0:Orders>Formula
Section titled “Formula”count(xPath('Orders/Order/Id', body))Result
Section titled “Result”2xPath returns the two order ids, 1337 and 1338.

Counting the Id elements gives the number of orders in the message, here 2.
Example 2: Count the message body
Section titled “Example 2: Count the message body”Message Body:
NodiniteFormula
Section titled “Formula”count(body)Result
Section titled “Result”1
The whole message body is one value, so count returns 1.
Example 3: Count a message context value
Section titled “Example 3: Count a message context value”Message Context:
| Key | Value |
|---|---|
id |
Nodinite |
Formula
Section titled “Formula”count(context('id'))Result
Section titled “Result”1
The context key id holds one value, so count returns 1.
Example 4: Count the values extracted from JSON
Section titled “Example 4: Count the values extracted from JSON”Message Body:
{a:[{b:'This'},{b:'is'},{b:'Formula Plugin'}]}Formula
Section titled “Formula”count(jsonPath('a[*].b', body))Result
Section titled “Result”3
jsonPath extracts This, is, and Formula Plugin, so count returns 3.
Example 5: Count values that contain duplicates
Section titled “Example 5: Count values that contain duplicates”Message Body:
{a:[{b:'This'},{b:'is'},{b:'is'},{b:'Formula Plugin'}]}Formula
Section titled “Formula”count(jsonPath('a[*].b', body))Result
Section titled “Result”3
The array holds four values, but is appears twice and is counted once, so count returns 3.
Example 6: Count values across several message context keys
Section titled “Example 6: Count values across several message context keys”Message Context:
| Key | Value |
|---|---|
id |
<Orders><Id>3</Id><Id>6</Id></Orders> |
id2 |
<Orders><Id>4</Id><Id>5</Id></Orders> |
Formula
Section titled “Formula”count(xPath('/Orders/Id', context(regex('id.*'))))Result
Section titled “Result”4MessageContext.png)
context(regex('id.*')) returns both XML values, and xPath extracts two ids from each, so count returns 4.
- When the content holds no values,
countreturns0, a value, not the warning The Expression yields no result. - Point xPath at a leaf element, such as
.../OrderLine/LineId. An element with child elements (.../OrderLine) gives no values, socount(xPath('OrderEntry/OrderLines/OrderLine', body))returns0.