Exercise - Create the reward level table object

Scenario

The Reward Level table will store the different reward levels and the points that are required to acquire an award level.

The table consists of two fields:

  • Level

  • Minimum Reward Points

Create the Reward Level table object

To create the Reward Level table object, follow these steps:

  1. Create a new .al file.

    a. To create a new table in your extension, first create a new file. You can create a new file by selecting the New File button in the side bar of Visual Studio Code.

    b. Make sure that the filename ends with .al. In this example, you might want to use the name Tab.RewardLevel.al.

  2. Add the code to the table. Copy the following code and paste it in the Tab.RewardLevel.al file.

table 50100 "Reward Level"
{
    Caption = 'Reward Level';
    TableType = Normal;
    DataClassification = CustomerContent;

    fields
    {
        field(1; Level; Text[20])
        {
            Caption = 'Level';
            DataClassification = CustomerContent;
        }

        field(2; "Minimum Reward Points"; Integer)
        {
            Caption = 'Minimum Reward Points';
            DataClassification = CustomerContent;
            MinValue = 0;
            NotBlank = true;

            trigger OnValidate();
            var
                tempPoints: Integer;
                RewardLevel: Record "Reward Level";
            begin
                tempPoints := "Minimum Reward Points";
                RewardLevel.SetRange("Minimum Reward Points", tempPoints);
                if RewardLevel.FindFirst then
                    Error('Minimum Reward Points must be unique');
            end;
        }
    }

    keys
    {
        key(PK; Level)
        {
            Clustered = true;
        }
        key("Minimum Reward Points"; "Minimum Reward Points") { }
    }

    trigger OnInsert();
    begin

        Validate("Minimum Reward Points");
    end;

    trigger OnModify();
    begin
        Validate("Minimum Reward Points");
    end;
}