Dynamics 365 Business Central: How to quickly check if a field is a primary key field
Explains how to check in AL whether a field belongs to a table's primary key. Besides reading the Key virtual table, you can copy two local procedures from codeunit 9800 User. They use RecordRef and KeyRef to read the first key, report the number of primary key fields, and return whether the field is one of them.

post Dynamics 365 Lab
"Every table has a primary key, and there can only be one primary key per table. Primary keys are defined on table objects only."
Key points
- Each table has exactly one primary key, defined only on table objects; any key defined in a table extension is a secondary key
- Secondary keys create SQL indexes and can be defined in both tables and table extensions
- Table 2000000063 Key can be used to check key fields, but codeunit 9800 User contains reusable helper procedures
- IsPrimaryKeyField opens the table with RecordRef, reads KeyIndex(1) and returns true if the field is in it, plus the count of primary key fields
- A test page extension on Customer List checks the No. field of Sales Header
Quotes
- “Every table has a primary key, and there can only be one primary key per table. Primary keys are defined on table objects only.” (Establishes the fundamental constraint that guides how developers structure table definitions and extensions)
AL objects mentioned
As named in the post. A name that matches one object page by exact type and name links to it; the others stay as named.
Not found in BC28-30: table “Key”.
Possibly related
No source states these links: they share an object, a model read them, or a person found the same subject.
- BC Friday Tips #56 Understand Missing Fields in Copy Documents — possibly related: both name Table 37 “Sales Line”
- BC Friday Tips #84 Why Is Your Item Missing from the Sales Line Lookup? — possibly related: both name Table 37 “Sales Line”
- Business Central Development Serie - Part 2b: AI for BC Development — The Knowledge Gap That Ships to Production — possibly related: both name Table 37 “Sales Line”
- Complete Cloud Migration to Business Central Online — possibly related: names Codeunit 9800 “User”
- Dynamics 365 Business Central: Add a Scan Barcode action and scan multiple/Continious scanning (Invoke barcode scanning programmatically from AL) — possibly related: both name Table 37 “Sales Line”
- Dynamics 365 Business Central: Entering Quantities/Amounts by Calculation and Negative Numbers — possibly related: both name Table 37 “Sales Line”
- Dynamics 365 Business Central: How to flow/pass custom field values from Sales Order to Production Order — possibly related: both name Table 37 “Sales Line”
- Dynamics 365 Business Central: How to note the records that the user has selected on the Subpage/Lines (Page.SetSelectionFilter in part) — possibly related: both name Table 37 “Sales Line”
- Dynamics 365 Business Central: Quickly get next Sequence/Entry Number (without using FindLast -> Entry No. + 1) — possibly related: both name Table 37 “Sales Line”
- Dynamics 365 Business Central: Use a default quantity of 1 for all types on documents – Customization — possibly related: both name Table 37 “Sales Line”
Context
- Features: primary key detection, key field identification, RecordRef API, KeyRef API
Source: Dynamics 365 Lab - Yun Zhu, community blog. Summary, key points and quotes are derived (CONTENT-NOTICE.md); read the original for the full text.