---
title: "Formula Search Field Expression Plugin: Language Rules and Function Reference"
description: The Formula Search Field Expression plugin combines functions to extract, transform, and test values from the body, context, and fields of a Log Event. Learn the formula language rules, nesting, and every function.
---

# :fa-function: Formula Search Field Expression Plugin

The **Formula** plugin extracts values from a [Log Event][] by combining functions, much like a formula in Microsoft Excel. Use it when a single plugin isn't enough: to extract and then transform a value, to combine values from the body and the context, to count or sum, or to look up a value in a SQL database.

* ✅ Extract from the message body, context properties, and Log Event fields in one expression  
* ✅ Nest functions: the result of one function is the content of the next  
* ✅ Convert, alter, test, and calculate on the extracted values  

![Build a nested Formula on an Order Received event and test it, animated walkthrough (7.12)][3]  
*Animated walkthrough: on the Order Received JSON of the [Order-to-Cash demo][], `concat(jsonPath('$.orderId', body), ' / ', toUpperCase(jsonPath('$.customer.name', body)))` nests three functions and returns one value, `ORD-10005 / TAILSPIN TOYS`.*  

>[!TIP]
>Start with a dedicated plugin such as [XPath][XPath plugin] or [JSON Path][JSON Path plugin] when it can do the job; see [Choose a plugin][]. Use Formula when you need to combine or transform.

---

## :fa-brain-circuit: How a Formula Is Evaluated

A formula is evaluated from the inside out. A **source** function reads data from the [Log Event][]; every other function takes **content** (the output of the function inside it) and returns new content. The final result becomes the values of the [Search Field][Search Fields]. You check the result in the **Test Expression** tab; see [Test Your Expression][].

```mermaid
graph LR
    A["Log Event"] --> B["Source<br/>body, context, field"]
    B --> C["Extract<br/>xPath, jsonPath, regex, csv"]
    C --> D["Convert, Alter,<br/>Logical, Math"]
    D --> E["Search Field values"]
    style A fill:#2678BD,color:#fff
    style B fill:#3F827B,color:#fff
    style C fill:#E47528,color:#fff
    style D fill:#9E59BB,color:#fff
    style E fill:#93d16d,color:#000
```

*Diagram: a formula reads a source from the Log Event (blue), extracts values (orange), optionally transforms or tests them (purple), and returns the Search Field values (green) that users search for in [Log Views][]. Each color is one function group from the [Function reference](#fa-book-function-reference).*

### Nesting, Step by Step

#### Input

```xml
<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

```text
count(xPath('Orders/Order/Id', body))
```

#### Evaluation

1. [body][] returns the whole message body.
2. `xPath('Orders/Order/Id', body)` ([xPath][]) returns two values: `1337` and `1338`. The path needs no namespace prefix.
3. `count(...)` ([count][]) counts them.

#### Result

```text
2
```

![Test Expression tab: count(xPath('Orders/Order/Id', body)) on a message with two orders returns 2 (7.12)][2]  
*The nested formula returns one value, `2`: one count per matching `Id` element, which is why the test bench reports one unique match.*  

---

## :fa-scroll: Formula language rules

These rules apply to every function. Function pages link here instead of repeating them.

|Rule|Details|Example|  
|---|---|---|  
|**Content**|The data a function works on: a source function (`body`, `context`, `field`) or the result of another function.|`toUpperCase(context('Region'))`|  
|**`body` has no parentheses**|Write `body`, not `body()`.|`regex('\d+', body)`|  
|**Strings use single quotes**|Every text argument is enclosed in `'…'`. Numbers are written as is.|`substring(0, 3, body)`|  
|**Escape a single quote by doubling it**|A literal `'` inside a string is written `''`.|`jsonPath('ord''erid', body)`|  
|**Backslashes are written once**|Regular expressions use normal .NET syntax; don't double backslashes.|`regex('\d+', body)`|  
|**`#` and spaces in regular expressions**|Formula regular expressions ignore unescaped whitespace and treat `#` as the start of a comment, and matching ignores case. Write a literal `#` as `[#]` or `\#` and a literal space as `\s` or `[ ]`. `'Order#.*'` silently runs as `'Order'`.|`context(regex('Order[#].*'))`|  
|**`context(regex(...))` must match the whole key**|The text the pattern matches is used as the key to read, so a partial match such as `'^Order[#]'` finds nothing. End the pattern with `.*`.|`context(regex('Order[#].*'))`|  
|**Field names are case-sensitive**|The name in `field('…')` must match the [Log Event][] field exactly; only function names ignore case.|`field('MessageGUID')`|  
|**Function names are case-insensitive**|`xpath`, `xPath`, and `XPATH` all work. The reference tables write them in camelCase; examples show them exactly as typed in their screenshot.|`jsonpath('a[*].b', body)`|  
|**Several values flow through**|Extract functions can return several values. The next function handles each value, and Math functions aggregate them.|`count(jsonPath('a[*].b', body))`|  
|**Search Field values are unique**|The values stored on a [Search Field][Search Fields] are unique; duplicates are collapsed. The [test bench][Test Your Expression] shows the **Number of unique matches**. Inside a formula, Math functions still see how often each value occurred; see `unique` below.|`3 6 6` → `3`, `6`|  
|**Logical functions return `true` or no value**|`equal`, `notEqual`, `startsWith`, and `endsWith` return `true` when the condition holds, and no value when it doesn't. When one argument returns several values, one match is enough; add an unquoted `true` as the last argument to require every value to match.|`startsWith('U', regex('GMT\|UTC', body))` → `true`|  
|**XPath ignores namespaces**|In [xPath][], element names match without namespace prefixes or declarations.|`xPath('Orders/Order/Id', body)` on `<ns0:Orders …>`|  

### Escaping Single Quotes

#### Input

```json
{"ord'erid":"123","name":"John Doe"}
```

#### Formula

```text
jsonpath('ord''erid',body)
```

#### Result

```text
123
```

![Escaped single quote in a jsonPath formula (7.12)][1]  
*The key `ord'erid` is written `ord''erid` inside the quoted JSONPath expression of [jsonPath][].*  

---

## :fa-book: Function reference

Each function has its own page with parameters, return values, and examples.

### Source

Read data from the [Log Event][].

|Function|Returns|  
|---|---|  
|[body][]|The whole message body as text|  
|[context('Key')][context]|The value of one context property|  
|[context(regex('Key pattern'))][context]|The values of all context properties whose whole key matches the regular expression|  
|[contextKey(regex('Key pattern'))][contextKey]|The keys (not the values) of the context properties that match|  
|[field('fieldName')][field]|The value of a Log Event field (case-sensitive name)|  

### Extract

Pick values out of structured or unstructured content.

|Function|Returns|  
|---|---|  
|[csv(headerRowsToSkip, 'delimiter', columnIndex, 'commentSymbol', content)][csv]|The values of one column (zero-based) of delimited text|  
|[jsonPath('expression', content)][jsonPath]|The values that match a JSONPath expression|  
|[jsonPathKey('expression', content)][jsonPathKey]|The property names (keys) that match a JSONPath expression|  
|[regex('expression', content)][regex]|The whole matches of a regular expression|  
|[regexGroup('expression', 'groups', content)][regexGroup]|The captured groups of a regular expression|  
|[sqllookup('connectionStringId', 'database', 'table', 'searchColumn', 'resultColumn', content)][sqllookup]|A value looked up in a SQL Server table|  
|[xPath('expression', content)][xPath]|The values that match an XPath 1.0 expression|  
|[xPath2('expression', content)][xPath2]|The values that match a full XPath expression over the whole document|  

### Convert

Change the encoding or case of content.

|Function|Returns|  
|---|---|  
|[base64decode(content [, 'source encoding'])][base64decode]|Decoded text from base64|  
|[base64encode(content [, 'input encoding'])][base64encode]|Base64 text|  
|[convert('source encoding', 'target encoding', content)][convert]|Content re-encoded from one character encoding to another|  
|[toLowerCase(content)][toLowerCase]|Lowercase text|  
|[toUpperCase(content)][toUpperCase]|Uppercase text|  

### Alter

Change text.

|Function|Returns|  
|---|---|  
|[concat(content, ..., content)][concat]|All arguments joined into one string|  
|[removebom(content)][removebom]|Content without a leading byte order mark (BOM)|  
|[replace('oldValue', 'newValue', content)][replace]|Content with every occurrence of a text replaced|  
|[replaceregex('pattern', 'newValue', content)][replaceregex]|Content with every regular expression match replaced|  
|[substring(startIndex, length, content)][substring]|Part of the text, from a zero-based start index|  

### Logical

Test content.

|Function|Returns|  
|---|---|  
|[equal('expression', content [, matchAllValues])][equal]|`true` when content equals the expression|  
|[notEqual('expression', content [, matchAllValues])][notEqual]|`true` when content differs from the expression|  
|[isnull(content, 'default')][isnull]|The default when content is null or empty, else the content|  
|[startsWith('expression', content [, matchAllValues])][startsWith]|`true` when content starts with the expression|  
|[endsWith('expression', content [, matchAllValues])][endsWith]|`true` when content ends with the expression|  

### Math

Aggregate and round values. The optional `unique` argument of the aggregating functions is an unquoted `true` or `false`: with `false`, a value that occurs several times counts every time (two invoice lines of `100` → `sum` = `200`); with `true`, identical values count once (→ `100`). Values are compared as text, so `100` and `100.00` are different. The default differs per function, as shown below; leave it `false` for totals. [round][] doesn't aggregate and has no `unique` argument.

|Function|Returns|  
|---|---|  
|[avg(content [, unique])][avg]|The average of the numeric values; `unique` defaults to `false`|  
|[count(content [, unique])][count]|The number of values; `unique` defaults to `true`|  
|[length(content [, unique])][length]|The length in characters of the content; `unique` defaults to `true`|  
|[max(content [, unique])][max]|The largest numeric value; `unique` defaults to `true`|  
|[min(content [, unique])][min]|The smallest numeric value; `unique` defaults to `true`|  
|[round(content, decimals)][round]|Every numeric value rounded to the given decimals, midpoints away from zero (:new7.12:)|  
|[sum(content [, unique])][sum]|The sum of the numeric values; `unique` defaults to `false`|  

---

## :fa-triangle-exclamation: Performance

* Every formula runs for every matching [Log Event][] during logging and [re-index][How a Re-Index Runs]; keep formulas as short as the job allows.
* Use [xPath][] (XPath 1.0, forward-only reader, see [Allowed XPath expressions][]) for large XML; use [xPath2][] only when you need look-back steps or predicates on child elements.
* `sqllookup` queries SQL Server for each evaluation; see [sqllookup][] before using it on high-volume [Message Types][].

## :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-puzzle-piece: [Search Field Expressions][]  
* :fa-signs-post: [Choose a plugin][]  
* :fa-magnifying-glass-plus: [Search Fields][]  
* :fa-file: [Message Types][]  
* :fa-envelope: [Log Event][]  
* :fa-language: [Character Encoding][]  
* :fa-wand-magic-sparkles: [Search Field Wizard][]  
* :fa-list-check: [Allowed XPath expressions][]  
* :fa-search: [Log Views][]  
* :fa-magnifying-glass: [Search with Log Views (end-user guide)][Log Overview]  

[1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/SingleQuotes.png
[2]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/count_Example1.png
[3]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Formula_Nesting.gif

[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
[Allowed XPath expressions]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Troubleshooting/Allowed%20XPath%20expressions.md
[avg]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/average.md
[base64decode]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Convert/base64decode.md
[base64encode]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Convert/base64encode.md
[body]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/body.md
[Character Encoding]:{ndr}/Core%20Services/Log%20Event/Features/Character%20Encoding.md
[Choose a plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Overview.md#fa-signs-post-choose-a-plugin
[concat]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Alter/concat.md
[Context]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/context.md
[contextKey]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/contextKey.md
[convert]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Convert/convert.md
[count]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/count.md
[csv]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/csv.md
[endsWith]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Logical/endsWith.md
[equal]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Logical/equal.md
[field]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/field.md
[How a Re-Index Runs]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md#how-a-re-index-runs
[isnull]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Logical/isnull.md
[JSON Path plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/JSON%20Path.md
[jsonPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/jsonPath.md
[jsonPathKey]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/JsonPathKey.md
[length]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/length.md
[Log Event]:{ndr}/Core%20Services/Log%20Event/Overview.md
[Log Overview]:{ndr}/Getting%20Started/2.%20Log/Overview.md
[Log Views]:{ndr}/Administration/1.%20Log%20Management/1.%20Log%20Views/Features/What%20is%20a%20Log%20View.md
[max]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/max.md
[Message Types]:{ndr}/Repository%20Model/Message%20Types/Overview.md
[min]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/min.md
[notEqual]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Logical/notEqual.md
[Order-to-Cash demo]:{ndr}/Repository%20Model/BPMs/Examples/Order-to-Cash/Generate-O2C-Demo-Data.md
[regex]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/regex.md
[regexGroup]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/regexGroup.md
[removebom]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Alter/removebom.md
[replace]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Alter/replace.md
[replaceregex]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Alter/replaceregex.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
[Search Field Wizard]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Getting%20started/How%20to%20use%20the%20Search%20Field%20Wizard.md
[Search Fields]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/What%20is%20a%20Search%20Field.md
[sqllookup]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/sqllookup.md
[startsWith]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Logical/startsWith.md
[substring]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Alter/substring.md
[sum]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/sum.md
[Test Your Expression]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md#test-your-expression
[toLowerCase]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Convert/toLowerCase.md
[toUpperCase]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Convert/toUpperCase.md
[xPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath.md
[XPath plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/XPath.md
[xPath2]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath2.md