Creating Tables in AL

Learn to define tables with fields, keys, triggers, and data classification in Business Central.

35 minBeginner

Learning Objectives

  • Create a table object with fields and keys
  • Apply DataClassification for GDPR compliance
  • Use TableRelation for lookups
  • Implement table triggers for business logic

Tables: The Foundation

In Business Central, tables store all application data. Every page, report, and codeunit ultimately reads from or writes to tables.

When you create an extension, tables are usually the first objects you define.

Basic Table Structure

ItemCategory.Table.al
table 50110 "Item Category Extension"
{
    Caption = 'Item Category Extension';
    DataClassification = CustomerContent;
    LookupPageId = "Item Categories";
    DrillDownPageId = "Item Categories";

    fields
    {
        field(1; "Code"; Code[20])
        {
            Caption = 'Code';
            NotBlank = true;
        }
        field(2; Description; Text[100])
        {
            Caption = 'Description';
        }
        field(3; "Parent Category"; Code[20])
        {
            Caption = 'Parent Category';
            TableRelation = "Item Category Extension";
        }
        field(4; "Sort Order"; Integer)
        {
            Caption = 'Sort Order';
        }
    }

    keys
    {
        key(PK; Code)
        {
            Clustered = true;
        }
        key(Sort; "Sort Order", Code)
        {
        }
    }
}

Field Properties

Common field properties you'll use daily:

PropertyPurpose
CaptionDisplay label (supports translations)
NotBlankRequired field validation
TableRelationLookup link to another table
EditableWhether user can modify
InitValueDefault value on insert
MinValue / MaxValueRange validation
DecimalPlacesFormat for decimal fields

Data Classification

Every field must have a DataClassification for GDPR compliance:

field(10; "Customer Email"; Text[80])
{
    Caption = 'Customer Email';
    DataClassification = EndUserIdentifiableInformation;
}
ClassificationWhen to Use
CustomerContentGeneral business data
EndUserIdentifiableInformationNames, emails, phone numbers
AccountDataLogin credentials
OrganizationIdentifiableInformationCompany registration data
SystemMetadataSystem-generated IDs

Mandatory Since BC 14

Fields without DataClassification will cause compilation warnings (and errors with CodeCop enabled).

Table Triggers

trigger OnInsert()
begin
    TestField("Customer No.");
    "Created Date" := Today;
end;

trigger OnModify()
begin
    "Modified Date" := Today;
end;

trigger OnDelete()
begin
    if HasRelatedRecords() then
        Error('Cannot deleterelated records exist.');
end;

trigger OnRename()
begin
    // Runs when primary key changes
end;

Field Triggers

Individual fields can have validation triggers:

field(5; "Bonus Percent"; Decimal)
{
    trigger OnValidate()
    begin
        if ("Bonus Percent" < 0) or ("Bonus Percent" > 100) then
            Error('Bonus percent must be between 0 and 100.');
    end;
}

Next Steps

Learn how to extend existing Microsoft tables without modifying the base application.

Open Source

Help improve this platform

NAVBC Learning is open source. Report issues, suggest improvements, or join discussions on GitHub.