Tables in Microsoft Dynamics BC AL Extension - Muhammad-Ali-Mughal/Microsoft-Dynamics-BC-AL-Extension GitHub Wiki

Tables in Microsoft Dynamics BC AL Extension

Let's start building the first extension of Microsoft Dynamics Business Central in Application Language. There are some prerequisites for this:

  • Knowledge of basic AL syntax
  • Microsoft Business Central access (On-prem/cloud sandbox)
  • Visual Studio Code
  • AL Extension installed in VS Code

Creating Tables

Building tables in AL is the most basic task in making BC Extensions. Tables are objects in BC that contain fields for the data to be entered by the users and saved in the same format. There are many data types in AL, but we'll be using some of the basic ones. Table, just like other objects in AL, has a unique ID starting from 50000. Following is the basic syntax of a table:

table 50100 "CRONUS Course"

{
    Caption = 'Course';
    DataClassification = CustomerContent;

    fields
    {
        field(1; Code; Code[10])
        {
            DataClassification = CustomerContent;
        }
        field(10; "Customer No."; Code[20])
        {
            Caption = 'Customer No.';
            TableRelation = Customer;
        }
        field(20; Name; Text[30])
        {
            Caption = 'Name';
            DataClassification = CustomerContent;
        }
        field(30; Address; Text[50])
        {
            Caption = 'Address';
            DataClassification = CustomerContent;
        }
        field(40; Email; Text[50])
        {
            Caption = 'Email';
            DataClassification = CustomerContent;
        }
        field(50; "Post Code"; Code[50])
        {
            Caption = 'Post Code';
            DataClassification = CustomerContent;
        }
        field(60; Country; Text[50])
        {
            Caption = 'Country';
            DataClassification = CustomerContent;
        }
    }

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

}

Here are some basic properties used in the table.

Caption

Caption is used in many objects in AL because of its function. The caption displays the output name of the object in which it is included. But something is interesting about captions that is if you don't provide the caption property then BC will still display the name of the object taken from the name you entered while declaring it.

DataClassification

Data classification is provided by Microsoft by default in BC. It simply means that in which category the data of the field is classified. The "ToBeClassified" value of DataClassification means that this data isn't classified yet in any category. I've used "CustomerContent" as the value of DataClassification which means that the data is of or related to the customer.

Fields

Inside the table object, there are fields defined along with their FieldId, Name, and DataType. The Syntax of the field is as follows:

field(fieldId; Name; DataType)
        {
            DataClassification = CustomerContent;
        }

The field is also an object just like others and without the Caption property, BC will display the "Name" as the output name of the field. The field is the object which will store the data coming from the page.

Keys

Just like other tables like SQL, no SQL, and others, AL tables also have keys, primary as well as secondary. Keys are defined in the following syntax:

key(Name; FieldName)
        {
            Clustered = true;
        }

Keys may be Clustered or not, which basically means that the primary keys will be placed in ascending order if Clustered.

This was the basic implementation of how to create tables in AL BC. Further, we'll be learning about pages and triggers to use in tables and pages.