---
title: "Formula - context: Read Message Context Property Values"
description: The context Formula function returns the value of a Message Context property of a Log Event, by exact key or by a regular expression on the key. Includes syntax, parameters, and examples.
---

# :fa-function: Formula - Context

The `context` [Formula][] function returns the value of one or more [Message Context properties][JSON Log Event - Context] (key/value pairs) of the [Log Event][], such as a correlation ID or an HTTP header.

![Read an order number and then every customer key from the Message Context of an Order-to-Cash event with context, animated walkthrough (7.12)][hero]  
*Animated walkthrough: on the logged Message Context of the Order Received event of order ORD-10005 in the [Order-to-Cash demo][], `context('OrderNumber')` returns `ORD-10005`, and `context(regex('Customer.*'))` returns the values of both `CustomerId` and `CustomerName`: `CUST-005` and `Tailspin Toys`.*  

## :fa-code: Syntax

```text
context('Key')
context(regex('Key pattern'))
```

### Parameters

|Parameter|Required|Description|  
|---|---|---|  
|`'Key'`|Yes (first form)|The exact name of the context property, in single quotes.|  
|`regex('Key pattern')`|Yes (second form)|A [regex][] on the key names. Every context property whose key matches contributes its value.|  

### Returns

* `context('Key')` returns the value of that context property, or nothing when the Log Event has no such key.
* `context(regex('Key pattern'))` returns the values of all matching context properties; duplicates are collapsed (see [Formula language rules][]).

To get the key names instead of the values, use [contextKey][].

## :fa-list-ol: Examples

### Example 1: Read the correlation ID

#### Input

|Key|Value|  
|---|---|  
|Correlation Id|demo001|  
|Header#Content-Type|Application/XML|  

#### Formula

```text
context('Correlation Id')
```

#### Result

```text
demo001
```

![Test Expression tab, Message Context: Context('Correlation Id') returns demo001 (7.12)][1]  
*The `Correlation Id` key matches exactly, so only its value `demo001` is returned. The screenshot writes `Context`; function names are case-insensitive.*  

### Example 2: Read the status of every order key

#### Input

|Key|Value|  
|---|---|  
|Order#123|OK|  
|Order#456|OK|  
|Order#789|Fail|  

#### Formula

```text
context(regex('Order[#].*'))
```

#### Result

```text
OK
Fail
```

All three keys match the pattern; the duplicate `OK` is collapsed into one unique value, so the test shows 2 unique matches.

* **Write a literal `#` as `[#]` or `\#`.** Formula regexes ignore unescaped whitespace and treat `#` as the start of a comment, so `'Order#.*'` silently runs as `'Order'` and returns nothing.
* **Match the whole key.** `context(regex(...))` reads the keys whose **matched text** equals a key name, so end the pattern with `.*`; `'^Order[#]'` matches only `Order#` and finds nothing.

See the [Formula language rules][] for the details.

![Test Expression tab: context with a regex on keys starting with Order and an escaped hash returns the unique values OK and Fail from three Order keys (7.12)][ex2]  
*Three keys match `Order[#].*`, but the two `OK` values collapse into one, so the test reports 2 unique matches.*  

### Example 3: Read XML stored in a context value

#### Input

|Key|Value|  
|---|---|  
|OrderNumber|`<Id>3</Id>`|  

#### Formula

```text
xpath('Id', context('OrderNumber'))
```

#### Result

```text
3
```

![Test Expression tab: xpath('Id', context('OrderNumber')) returns 3 from the XML stored in the OrderNumber key (7.12)][ex3]  
*`context` returns the XML text stored in `OrderNumber`, and [xpath][xPath] reads the `Id` element from it; the same pattern works with [jsonPath][] for JSON values.*  

## :fa-circle-info: Notes

* The context key in `context('Key')` must match the stored key name exactly, including spaces and prefixes such as `Header#`.
* `context` reads the Message Context only; it never reads the message body. Use [body][] for the payload.

## :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-envelope: [body][]  
* :fa-tags: [contextKey][]  
* :fa-table-list: [field][]  
* :fa-function: [Formula][]  
* :fa-scroll: [Formula language rules][]  
* :fa-puzzle-piece: [Search Field Expressions][]  
* :fa-envelope: [Log Event][]  
* :fa-magnifying-glass: [Message Context Key plugin][]  
* :fa-magnifying-glass: [RegEx on Message Context plugin][]  
* :fa-magnifying-glass: [XPath on Message Context plugin][]  
* :fa-key: [Context Properties tab in a Log View][Context Properties tab]  

[1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/Context.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
[body]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/body.md
[Context Properties tab]:{ndr}/Getting%20Started/2.%20Log/See%20the%20details%20of%20a%20Log%20Event.md#context-properties
[contextKey]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/contextKey.md
[ex2]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/context_Example2.png
[ex3]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/context_Example3.png
[field]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/field.md
[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
[hero]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/Formula_Context.gif
[JSON Log Event - Context]:{ndr}/Core%20Services/Log%20Event/Features/JSON%20Log%20Event.md#context
[jsonPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/jsonPath.md
[Log Event]:{ndr}/Core%20Services/Log%20Event/Overview.md
[Message Context Key plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Message%20Context%20Key.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
[RegEx on Message Context plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/RegEx%20on%20Message%20Context.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
[XPath on Message Context plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/XPath%20on%20Message%20Context.md