community postAL Developmentcommunity - not Microsoftreviewed - checked by Opus

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

Yun Zhu · 2025-11-28 · 498 words · about 2 min

"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”.

No source states these links: they share an object, a model read them, or a person found the same subject.

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.