---
title: "Formula - contextKey: Return Matching Message Context Key Names"
description: The contextKey Formula function returns the key names of the Message Context properties whose key matches a regular expression. Includes syntax, parameters, and an order example.
---

# :fa-function: Formula - ContextKey

The `contextKey` [Formula][] function returns the key names (not the values) of the [Message Context properties][JSON Log Event - Context] of the [Log Event][] whose key matches a regular expression.

![List the customer context keys of an order with contextKey, then count them, animated walkthrough (7.12)][2]  
*Animated walkthrough: on the Message Context of the Order Received event of order ORD-10005 in the [Order-to-Cash demo][], `contextKey(regex('^Customer'))` returns the key names `CustomerId` and `CustomerName`, not their values. Nested in [count][], the same call returns `2`.*  

## :fa-code: Syntax

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

### Parameters

|Parameter|Required|Description|  
|---|---|---|  
|`regex('Key pattern')`|Yes|A [regex][] that is matched against the context key names.|  

### Returns

Every matching key name, one value per key. Duplicates are collapsed (see [Formula language rules][]). When no key matches, nothing is returned. To get the values of the matching context keys instead, use [context][Context] with the same `regex(...)` argument.

## :fa-list-ol: Examples

### Example 1: List the order keys in the context

#### Input

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

#### Formula

```text
contextKey(regex('Order#.*'))
```

#### Result

```text
Order#123
Order#456
Order#789
```

![Formula contextKey expression returning three Order key names from the Message Context tab (7.12)][1]  
*Each key that matches `Order#.*` is returned as its own unique match.*  

## :fa-circle-info: Notes

* `contextKey` returns the whole key name whenever the pattern matches part of it. The pattern matches anywhere in the key name unless you anchor it: use `^Order[#]` to match only keys that start with `Order#`.
* In Formula regular expressions `#` starts a comment, so `'Order#.*'` in the example really runs as `'Order'`. It works here because every key starts with `Order`; write `[#]` or `\#` when the `#` matters. See [Formula language rules][].
* Combine with [count][] to get the number of matching keys, for example `count(contextKey(regex('Order#.*')))`.

## :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-key: [context][Context]  
* :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-key: [Context Properties tab in a Log View][Context Properties tab]  

[1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/ContextKey.png
[2]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Source/Formula_ContextKey.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
[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
[Context Properties tab]:{ndr}/Getting%20Started/2.%20Log/See%20the%20details%20of%20a%20Log%20Event.md#context-properties
[count]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/count.md
[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
[JSON Log Event - Context]:{ndr}/Core%20Services/Log%20Event/Features/JSON%20Log%20Event.md#context
[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
[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