Exercise - Add tables and pages for master data
Scenario
You're a developer working for CRONUS International Ltd. You've been asked to extend the example setup extension. The requirement is to add a corresponding master and supplemental table and all appropriate pages and design patterns.
Tasks
Add two new tables to the Example Application extension.
Create pages for the Example and Example Type tables.
Apply the No. Series Design Pattern.
Steps
In VS Code, in your extension, add a new folder named: MasterData. All new files are to be added in this new folder.
Create a new file, named: Example.Table.al.
Create a new table in this file by using code snippets. Enter ttable and then press the Tab key.
Change the ID to 50125 and the name to Example.
Set the DataClassification property to CustomerContent and set the Caption property to Example.
Remove the MyField field.
Create the following fields. You can use the tfield snippet.
Field No. Field name Data type Length 1 No. Code 20 2 Description Text 50 3 Example Type Code Code 10 4 No. Series Code 20 Set the DataClassification property for each field in the table to CustomerContent.
Set the Caption property for each field in the table.
Set the Primary Key to No.
Remove the global var section.
Remove the table triggers.
Your code should now resemble this:
table 50125 Example { DataClassification = CustomerContent; Caption = 'Example'; fields { field(1; "No."; Code[20]) { DataClassification = CustomerContent; Caption = 'No.'; } field(2; "Description"; Text[50]) { DataClassification = CustomerContent; Caption = 'Description'; } field(3; "Example Type Code"; Code[10]) { DataClassification = CustomerContent; Caption = 'Example Type Code'; } field(4; "No. Series"; Code[20]) { DataClassification = CustomerContent; Caption = 'No.'; } } keys { key(Pk; "No.") { Clustered = true; } } }
Create a new file named: ExampleType.Table.al.
Create a new table in this file by using code snippets. Enter ttable and then press the Tab key.
Change the ID to 50126 and the name to ExampleType.
Set the DataClassification property to CustomerContent and set the Caption property to Example Type.
Remove the MyField field.
Create the following fields. You can use the tfield snippet.
Field No. Field name Data type Length 1 Code Code 10 2 Description Text 50 Set the DataClassification property for each field in the table to CustomerContent.
Set the Caption property for each field in the table.
Set the Primary Key to Code.
Remove the global var section.
Remove the table triggers.
Your code should now resemble this:
table 50126 ExampleType { DataClassification = CustomerContent; Caption = 'Example Type'; fields { field(1; Code; code[10]) { DataClassification = CustomerContent; Caption = 'Code'; } field(2; Description; Text[50]) { DataClassification = CustomerContent; Caption = 'Description'; } } keys { key(Pk; Code) { Clustered = true; } } }
Open the Example.Table.al file and set the TableRelation property of the Example Type Code field to the Example Type table.
field(3; "Example Type Code"; Code[10]) { DataClassification = CustomerContent; Caption = 'Example Type Code'; TableRelation = ExampleType; }
If you don't have a table named: Example Setup in your project, then create one. (filename: ExampleSetup.Table.al)
The code should be as follows:
table 50124 "Example Setup" { Caption = 'Example Setup'; DataClassification = CustomerContent; fields { field(1; "Primary Key"; Code[10]) { Caption = 'Primary Key'; } field(2; "Example Nos."; Code[20]) { Caption = 'Example Nos.'; TableRelation = "No. Series"; } } keys { key(Key1; "Primary Key") { Clustered = true; } } }
If you don't have a page Example Setup Card in your project, then create one (filename: ExampleSetupCard.Page.al):
page 50101 ExampleSetupCard { ApplicationArea = All; Caption = 'Example Setup Card'; PageType = Card; SourceTable = "Example Setup"; UsageCategory = Administration; DeleteAllowed = false; InsertAllowed = false; layout { area(content) { group(General) { Caption = 'General'; field("Example Nos."; Rec."Example Nos.") { ToolTip = 'Specifies the value of the Example Nos. field.'; } } } } trigger OnOpenPage() begin if not rec.get() then rec.Insert(); end; }
Create two global variables in the Example table object.
var NoSeriesManagement: Codeunit NoSeriesManagement; ExampleSetup: Record "Example Setup";
Write code in the OnInsert trigger of the Example table to initialize the number series.
trigger OnInsert(); begin if "No." = '' then begin ExampleSetup.Get(); ExampleSetup.TestField("Example Nos."); NoSeriesManagement.InitSeries(ExampleSetup."Example Nos.", xRec."No. Series", 0D, "No.", "No. Series"); end; end;
Create an AssistEdit trigger and write code to enable selection by using the number series.
procedure AssistEdit(OldExample: Record Example): Boolean var Example: Record Example; begin Example := Rec; ExampleSetup.Get(); ExampleSetup.TestField("Example Nos."); if NoSeriesManagement.SelectSeries(ExampleSetup."Example Nos.", OldExample."No. Series", Example."No. Series") then begin NoSeriesManagement.SetSeries(Example."No."); Rec := Example; exit(true); end; end;
The Example table should now resemble this:
table 50125 Example { DataClassification = CustomerContent; Caption = 'Example'; fields { field(1; "No."; Code[20]) { DataClassification = CustomerContent; Caption = 'No.'; } field(2; "Description"; Text[50]) { DataClassification = CustomerContent; Caption = 'Description'; } field(3; "Example Type Code"; Code[10]) { DataClassification = CustomerContent; Caption = 'Example Type Code'; TableRelation = ExampleType; } field(4; "No. Series"; Code[20]) { DataClassification = CustomerContent; Caption = 'No.'; } } keys { key(Pk; "No.") { Clustered = true; } } var ExampleSetup: Record "Example Setup"; NoSeriesManagement: Codeunit NoSeriesManagement; trigger OnInsert(); begin if "No." = '' then begin ExampleSetup.Get(); ExampleSetup.TestField("Example Nos."); NoSeriesManagement.InitSeries(ExampleSetup."Example Nos.", xRec."No. Series", 0D, "No.", "No. Series"); end; end; procedure AssistEdit(OldExample: Record Example): Boolean var Example: Record Example; begin Example := Rec; ExampleSetup.Get(); ExampleSetup.TestField("Example Nos."); if NoSeriesManagement.SelectSeries(ExampleSetup."Example Nos.", OldExample."No. Series", Example."No. Series") then begin NoSeriesManagement.SetSeries(Example."No."); Rec := Example; exit(true); end; end; }
Create a new file named: ExampleCard.Page.al.
Create a new page in this file by using code snippets. Enter tpage and then press the Tab key.
Change the ID to 50125 and the name to Example Card.
Set the SourceTable property to Example and set the Caption property to Example Card.
Set the PageType property to Card and set the UsageCategory property to None. Remove the ApplicationArea property.
Verify that a content area is created in the layout section. If not, create an area called content.
Change the name of the first group to General. Set the Caption property of the group to General.
Add all fields to the General group, except the No. Series field.
Verify that the ApplicationArea property is set to All for all fields on the page.
Add meaningful tooltips for every field.
Add an OnAssistEdit trigger for the No. field.
In the OnAssistEdit trigger of the No. field, write code to run the AssistEdit function on the table level.
page 50125 "Example Card" { PageType = Card; UsageCategory = None; SourceTable = Example; Caption = 'Example Card'; layout { area(Content) { group(General) { Caption = 'General'; field("No."; Rec."No.") { ApplicationArea = All; ToolTip = 'Specifies the value of the No. field.'; trigger OnAssistEdit() begin if Rec.AssistEdit(xRec) then CurrPage.Update(); end; } field(Description; Rec.Description) { ApplicationArea = All; ToolTip = 'Specifies the value of the Description field.'; } field("Example Type Code"; Rec."Example Type Code") { ApplicationArea = All; ToolTip = 'Specifies the value of the Example Type Code field.'; } } } } }
Add a new file, named: ExampleList.Page.al.
Create a new page in this file by using code snippets. Enter tpage and then press the Tab key.
Change the ID to 50126 and the name to Example List.
Set the SourceTable property to Example and set the Caption property to Example List.
Set the PageType property to List and set the UsageCategory property to Lists. Verify that the ApplicationArea property is set to All.
Set the Editable property to false and set the CardPageID property to Example Card.
Verify that a content area is created in the layout section. If not, create an area called content.
Make sure there's a repeater with the name General.
Add all fields within the repeater, except the No. Series field.
Verify that the ApplicationArea property is set to All for all fields on the page.
Add meaningful tooltips for every field in the page.
Remove the actions area from the page.
Remove the factbox area from the page.
The code should now resemble this:
page 50126 "Example List" { PageType = List; ApplicationArea = All; UsageCategory = Lists; SourceTable = Example; Caption = 'Example List'; layout { area(Content) { repeater(General) { field("No."; Rec."No.") { ToolTip = 'Specifies the value of the No. field.'; } field(Description; Rec.Description) { ToolTip = 'Specifies the value of the Description field.'; } field("Example Type Code"; Rec."Example Type Code") { ToolTip = 'Specifies the value of the Example Type Code field.'; } } } } }
Add a new file, named: ExampleTypes.Page.al.
Create a new page in this file by using code snippets. Enter tpage and then press the Tab key.
Change the ID to 50124 and the name to Example Types.
Set the SourceTable property to Example Type and set the Caption property to Example Types.
Set the PageType property to List and set the UsageCategory property to Lists. Verify that the ApplicationArea property is set to All.
Set the Editable property to true.
Verify that a content area is created in the layout section. If not, create an area called content.
Make sure there's a repeater with the name General.
Add all fields within the repeater.
Verify that the ApplicationArea property is set to All for all fields on the page.
Add meaningful tooltips to all fields.
Remove the actions area from the page.
Remove the factbox area from the page.
The code now resembles this:
page 50124 "Example Types" { PageType = List; ApplicationArea = All; UsageCategory = Lists; SourceTable = ExampleType; Caption = 'Example Types'; Editable = true; layout { area(Content) { repeater(General) { field("Code"; Rec."Code") { ToolTip = 'Specifies the value of the Code field.'; } field(Description; Rec.Description) { ToolTip = 'Specifies the value of the Description field.'; } } } } }
Open the Example.Table.al file and set the LookupPageID and DrillDownPageID properties to Example List.
Open the ExampleType.Table.al file and set the LookupPageID and DrillDownPageID properties to Example Types.
Open the Example List page, and set the CardPageId property to Example Card and Editable to false.
Open the launch.json file in the .vscode folder. Set the startupObjectId setting to 50126 and the startupObjectType to Page.
Publish your extension to the sandbox. Select View > Command Palette... (Ctrl+Shift+P).
Enter AL: Publish in the search box (or press the F5 key) and then select the command from the list.
Verify that the Dynamics 365 Business Central application launches and that the Example List page appears. You can add new examples to the list by using the Example Card and add Example Types if you want to test your extension.
You can also test the No. Series functionality. For each new example that you add, a new number should be generated automatically, if you set a number series for examples in the Example Setup Card.