Record.FieldOrDefault
RecordReturns the value of a named field from a record, or a default value if the field does not exist.
Syntax
Record.FieldOrDefault(record as record, field as text, optional defaultValue as nullable any) as nullable anyParameters
| Name | Type | Required | Description |
|---|---|---|---|
record | record | Yes | The record to retrieve the field from. |
field | text | Yes | The name of the field to retrieve. |
defaultValue | any | No | The value to return when the field does not exist. Defaults to null. |
Return Value
any — The value of the specified field, or defaultValue (null if omitted) when the field is absent.
Remarks
Record.FieldOrDefault is the safe alternative to direct field access (record[FieldName]) and Record.Field. Direct field access raises an error if the field does not exist; Record.FieldOrDefault returns null by default or a custom fallback value you specify.
This is essential when working with semi-structured or inconsistent data — for example, JSON expanded into records where some rows have fields that others do not, or API responses where optional fields are absent rather than null.
The comparison to related approaches:
| Approach | Missing field behavior |
|---|---|
| record[FieldName] | Raises error |
| Record.Field(record, name) | Raises error |
| Record.FieldOrDefault(record, name) | Returns null |
| Record.FieldOrDefault(record, name, default) | Returns default value |
The defaultValue parameter accepts any type and defaults to null when omitted. You can use any expression as the default, including function calls.
Examples
Example 1: Retrieve a field that exists — returns its value
Record.FieldOrDefault([Name = "Alice", Age = 30], "Name")Result | |
|---|---|
| 1 | Alice |
Example 2: Field is absent — returns null by default
Record.FieldOrDefault([Name = "Alice", Age = 30], "Email")Result | |
|---|---|
| 1 | null |
Example 3: Specify a custom fallback value for missing fields
Record.FieldOrDefault([Name = "Alice"], "Email", "no-email@example.com")Result | |
|---|---|
| 1 | no-email@example.com |
Example 4: Safely extract an optional "Region" field from each Sales row
Table.AddColumn(
Table.SelectColumns(Table.FirstN(Sales, 4), {"OrderID", "CustomerName"}),
"Region",
each Record.FieldOrDefault(_, "Region", "Unknown"),
type text
)OrderID | CustomerName | Region | |
|---|---|---|---|
| 1 | 1 | Alice | East |
| 2 | 2 | Bob | West |
| 3 | 3 | Charlie | East |
| 4 | 4 | Alice | North |