- 2 minutes to read

Formula - ContextKey

The contextKey Formula function returns the key names (not the values) of the Message Context properties 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)
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.

Syntax

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 with the same regex(...) argument.

Examples

Example 1: List the order keys in the context

Input

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

Formula

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

Result

Order#123
Order#456
Order#789

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

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#.*'))).

Next Steps