---
id: post/yzhums-com/64478
type: post
title: "Dynamics 365 Business Central: How to quickly check if a field is a primary key field"
summary: 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.
tier: community
language: en
tags:
  - al development
  - table design
  - keys
  - primary keys
  - field validation
system: development
review:
  state: reviewed
  by: opus
  at: "2026-10-26T06:41:30.725Z"
  flags: []
generated:
  at: "2026-11-19T00:15:47.846Z"
  pipeline: 0.2.0
  prompts:
    extract-post: 1
  input_hash: a1db4415906be1428e4c4edd017d60645de49ee4f3c4c232093c84ecb6724710
evidence:
  - kind: blog
    url: https://yzhums.com/64478/
    title: "Dynamics 365 Business Central: How to quickly check if a field is a primary key field"
    date: "2025-11-28"
    commit: null
    t: null
    quote: null
  - kind: blog
    url: https://yzhums.com/64478/
    title: "Dynamics 365 Business Central: How to quickly check if a field is a primary key field"
    date: "2025-11-28"
    commit: null
    t: null
    quote: Every table has a primary key, and there can only be one primary key per table. Primary keys are defined on table objects only.
links:
  learn: []
  objects:
    - object/table/36
    - object/table/37
    - object/codeunit/9800
  features: []
  topics: []
  localizations: []
  videos: []
  posts: []
  guidelines: []
  related:
    - to: post/thatnavguy-com/https-thatnavguy-com-blog-2025-bc-friday-tips-56-understand-missing-fields-in-copy-documents--e98f782aea
      title: "BC Friday Tips #56 Understand Missing Fields in Copy Documents"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: post/thatnavguy-com/https-thatnavguy-com-blog-2026-bc-friday-tips-84-item-missing-from-sales-line-lookup--ac89a8fbc6
      title: "BC Friday Tips #84 Why Is Your Item Missing from the Sales Line Lookup?"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: post/kine-cz/https-blog-kine-cz-posts-bcdevelopmentserie-02b--0078d7faba
      title: "Business Central Development Serie - Part 2b: AI for BC Development — The Knowledge Gap That Ships to Production"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: https://learn.microsoft.com/dynamics365/business-central/dev-itpro/administration/migration-finish
      title: Complete Cloud Migration to Business Central Online
      label: possibly related
      why: names Codeunit 9800 "User"
      by: code
    - to: post/yzhums-com/70008
      title: "Dynamics 365 Business Central: Add a Scan Barcode action and scan multiple/Continious scanning (Invoke barcode scanning programmatically from AL)"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: post/yzhums-com/56173
      title: "Dynamics 365 Business Central: Entering Quantities/Amounts by Calculation and Negative Numbers"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: post/yzhums-com/74172
      title: "Dynamics 365 Business Central: How to flow/pass custom field values from Sales Order to Production Order"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: post/yzhums-com/25134
      title: "Dynamics 365 Business Central: How to note the records that the user has selected on the Subpage/Lines (Page.SetSelectionFilter in part)"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: post/yzhums-com/31884
      title: "Dynamics 365 Business Central: Quickly get next Sequence/Entry Number (without using FindLast -> Entry No. + 1)"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
    - to: post/yzhums-com/66212
      title: "Dynamics 365 Business Central: Use a default quantity of 1 for all types on documents – Customization"
      label: possibly related
      why: both name Table 37 "Sales Line"
      by: code
post_id: "64478"
source_id: yzhums-com
source_name: Dynamics 365 Lab - Yun Zhu
url: https://yzhums.com/64478/
published_at: "2025-11-28T02:29:20.000Z"
author: Yun Zhu
full_text: false
words: 498
quotes:
  - text: Every table has a primary key, and there can only be one primary key per table. Primary keys are defined on table objects only.
    why_it_matters: Establishes the fundamental constraint that guides how developers structure table definitions and extensions
code_objects_mentioned:
  - table Sales Header
  - table Sales Line
  - table Key
  - codeunit User
systems:
  - development
versions_mentioned: []
preview:
  embeddable: true
  frame_url: null
  image: https://yzhums.com/wp-content/uploads/2025/11/Snipaste_2025-11-28_11-28-38.png
  image_alt: null
  image_w: null
  image_h: null
  site_name: Dynamics 365 Lab
  favicon: https://yzhums.com/wp-content/uploads/2020/08/cropped-logo-3-32x32.png
  probed_at: "2026-10-26T06:11:29.101Z"
---

# Dynamics 365 Business Central: How to quickly check if a field is a primary key field

[Read the post](https://yzhums.com/64478/) · Dynamics 365 Lab - Yun Zhu (Yun Zhu, MVP) · 2025-11-28 · 498 words · tier community · reviewed (checked by Opus)

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

## 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.

- [table 36 "Sales Header"](../../objects/table/36.md)
- [table 37 "Sales Line"](../../objects/table/37.md)
- table "Key"
- [codeunit 9800 "User"](../../objects/codeunit/9800.md)

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](../../posts/thatnavguy-com/https-thatnavguy-com-blog-2025-bc-friday-tips-56-understand-missing-fields-in-copy-documents--e98f782aea.md) — possibly related: both name Table 37 "Sales Line"
- [BC Friday Tips #84 Why Is Your Item Missing from the Sales Line Lookup?](../../posts/thatnavguy-com/https-thatnavguy-com-blog-2026-bc-friday-tips-84-item-missing-from-sales-line-lookup--ac89a8fbc6.md) — 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](../../posts/kine-cz/https-blog-kine-cz-posts-bcdevelopmentserie-02b--0078d7faba.md) — possibly related: both name Table 37 "Sales Line"
- [Complete Cloud Migration to Business Central Online](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/administration/migration-finish) — 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)](../../posts/yzhums-com/70008.md) — possibly related: both name Table 37 "Sales Line"
- [Dynamics 365 Business Central: Entering Quantities/Amounts by Calculation and Negative Numbers](../../posts/yzhums-com/56173.md) — 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](../../posts/yzhums-com/74172.md) — 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)](../../posts/yzhums-com/25134.md) — possibly related: both name Table 37 "Sales Line"
- [Dynamics 365 Business Central: Quickly get next Sequence/Entry Number (without using FindLast -> Entry No. + 1)](../../posts/yzhums-com/31884.md) — possibly related: both name Table 37 "Sales Line"
- [Dynamics 365 Business Central: Use a default quantity of 1 for all types on documents – Customization](../../posts/yzhums-com/66212.md) — 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.
