Exercise - Create example documents

Completed

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 document table and all appropriate pages and design patterns.

Tasks

  • Create document tables (header and lines).

  • Create a document page, document list, and a document line subform.

  • Link the subform in the document page and set the correct properties.

  • Implement the No. Series Pattern.

Steps

  1. In VS Code, in your extension, create a new folder: Documents. All files in this exercise are to be created in this folder.

  2. Create a new file named: ExampleHeader.Table.al.

  3. Create a new table in this file by using code snippets. Enter ttable and then press the Tab key.

  4. Change the ID to 50127 and the name to ExampleHeader.

  5. Set the DataClassification property to CustomerContent and set the Caption property to Example Header.

  6. Remove the MyField field.

  7. Create the following fields. Use the tfield snippet.

    Field No. Field name Data type Length
    1 No. Code 20
    2 Document Date
    3 No. Series Code 20
    10 Posting Date Date
    13 No. Printed Integer
  8. Set the DataClassification property for each field in the table to CustomerContent.

  9. Set the Caption property for each field in the table.

  10. Set the Primary Key to No.

  11. Set the Editable property for the No. Printed field to false.

  12. Remove the global var section.

  13. Remove the table triggers.

  14. Your code now resembles:

    table 50127 ExampleHeader
    {
        DataClassification = CustomerContent;
        Caption = 'Example Header';
        fields
        {
            field(1; "No."; Code[20])
            {
                DataClassification = CustomerContent;
                Caption = 'No.';
            }
            field(2; "Document Date"; Date)
            {
                DataClassification = CustomerContent;
                Caption = 'Document Date';
            }
            field(3; "No. Series"; Code[20])
            {
                DataClassification = CustomerContent;
                Caption = 'No. Series';
            }
            field(4; "Posting Date"; Date)
            {
                DataClassification = CustomerContent;
                Caption = 'Posting Date';
            }
            field(5; "No. Printed"; Integer)
            {
                DataClassification = CustomerContent;
                Caption = 'No. Printed';
                Editable = false;
            }
        }
    
        keys
        {
            key(Pl; "No.")
            {
                Clustered = true;
            }
        }
    }
    
  15. Create a new file named: ExampleLine.Table.al.

  16. Create a new table in this file by using code snippets. Enter ttable and then press the Tab key.

  17. Change the ID to 50128 and the name to ExampleLine.

  18. Set the DataClassification property to CustomerContent and set the Caption property to Example Line.

  19. Remove the MyField field.

  20. Create the following fields. Use the tfield snippet.

    Field No. Field name Data type Length
    1 Document No. Code 20
    2 Line No. Integer
    3 Example No. Code 20
    4 Line Date Date
    6 Quantity Decimal
    7 Example Description Text 50
  21. Set the DataClassification property for each field in the table to CustomerContent.

  22. Set the Caption property for each field in the table.

  23. Set the Primary Key to Document No. and Line No.

  24. Remove the global var section.

  25. Remove the table triggers.

  26. Open the ExampleSetup.Table.al file and add a new field to the table.

    Field No. Field name Data type Length
    3 Document Nos. Code 20
  27. The Example Setup table now resembles:

    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";
            }
            field(3; "Document Nos."; Code[20])
            {
                Caption = 'Document Nos.';
                TableRelation = "No. Series";
            }
        }
        keys
        {
            key(Key1; "Primary Key")
            {
                Clustered = true;
            }
        }
    }
    
  28. Now add the Document Nos. field to the Example Setup Card:

    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.';
                    }
                    field("Document Nos."; Rec."Document Nos.")
                    {
                        ToolTip = 'Specifies the value of the Document Nos. field.';
                    }
                }
            }
        }
        trigger OnOpenPage()
        begin
            if not rec.get() then
                rec.Insert();
        end;
    }
    
  29. Open the ExampleHeader.Table.al file and create four global variables in the Example Header table object.

    var
       ExampleSetup: Record "Example Setup";
       NoSeriesManagement: Codeunit NoSeriesManagement;
    
    
  30. Write code in the OnInsert trigger of the Example Header table to initialize number series.

    trigger OnInsert();
     begin
         if "No." = '' then begin
             ExampleSetup.Get();
             ExampleSetup.TestField("Document Nos.");
             NoSeriesManagement.InitSeries(ExampleSetup."Document Nos.",
                                         xRec."No. Series",
                                         0D,
                                         "No.",
                                         "No. Series");
         end;
         InitRecord();
     end;
    
  31. Create an AssistEdit trigger and write code to enable selection by using the number series.

    procedure AssistEdit(OldExampleHeader: Record "ExampleHeader"): Boolean
     var
         ExampleHeader: Record "ExampleHeader";
     begin
         ExampleHeader := Rec;
         ExampleSetup.Get();
         ExampleSetup.TestField("Document Nos.");
         if NoSeriesManagement.SelectSeries(ExampleSetup."Document Nos.",
                                         OldExampleHeader."No. Series",
                                         ExampleHeader."No. Series") then begin
             NoSeriesManagement.SetSeries(ExampleHeader."No.");
             Rec := ExampleHeader;
             exit(true);
         end;
     end;
    
  32. Create an InitRecord trigger and write code to initialize other fields.

    procedure InitRecord()
     begin
         if Rec."Posting Date" = 0D then
             Rec."Posting Date" := WorkDate();
         Rec."Document Date" := WorkDate();
     end;
    
  33. Open the ExampleLine.Table.al file and add a TableRelation property on the Document No. field to the Example Header table.

  34. Add a TableRelation property on the Example No. field to the Example table.

  35. Set the DecimalPlaces property on the Quantity field to 0:2.

  36. Set the FieldClass property on the Example Description field to FlowField and the CalcFormula property to the following setting:

    CalcFormula = lookup(Example.Description where("No." = field("Example No.")));
    
  37. Remove the DataClassification property from the Example Description field.

  38. Create a new file named: ExampleDocument.Page.al.

  39. Create a new page in this file by using code snippets. Enter tpage and then press the Tab key.

  40. Change the ID to 50127 and the name to Example Document.

  41. Set the SourceTable property to Example Header and set the Caption property to Example Document.

  42. Set the PageType property to Document and set the UsageCategory property to Documents. Remove the ApplicationArea property.

  43. Verify that a content area is created in the layout section. If not, create an area called content.

  44. Change the name of the first group to General. Set the Caption property of the group to General.

  45. Add all fields to the General group, except the No. Series field.

  46. Verify that the ApplicationArea property is set to All for all fields on the page.

  47. Add meaningful tooltips for all fields on the page.

  48. In the OnAssistEdit trigger of the No. field, write code to run the AssistEdit function on the table level.

                 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;
                 }
    
  49. Remove the factboxes and actions from the page.

  50. Your page now resembles:

    page 50127 "Example Document"
    {
        PageType = Document;
        UsageCategory = Documents;
        SourceTable = ExampleHeader;
        Caption = 'Example Document';
    
        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("Document Date"; Rec."Document Date")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Document Date field.';
                    }
                    field("Posting Date"; Rec."Posting Date")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Posting Date field.';
                    }
                    field("No. Printed"; Rec."No. Printed")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the No. Printed field.';
                    }
                }
            }
        }
    }
    
  51. Create a new file named: ExampleLineSubpage.Page.al.

  52. Create a new page in this file by using code snippets. Enter tpage and then press the Tab key.

  53. Change the ID to 50128 and the name to Example Line Subform.

  54. Set the SourceTable property to ExampleLine and set the Caption property to Example Line Subpage.

  55. Set the PageType property to ListPart and set the UsageCategory property to None. Remove the ApplicationArea property.

  56. Set the AutoSplitKey and DelayedInsert properties to true.

  57. Verify that a content area is created in the layout section. If not, create an area called content.

  58. Remove the group in the area content and add a repeater with the name General.

  59. Add all fields within the repeater.

  60. Verify that the ApplicationArea property is set to All for all fields on the page.

  61. Make sure all fields have a meaningful tooltip.

  62. Make the Document No. and Line No. fields not editable.

  63. Remove the factboxes area.

  64. Remove the actions area.

  65. Your code now resembles:

    page 50128 "Example Line Subpage"
    {
        PageType = ListPart;
        Caption = 'Example Line Subpage';
        UsageCategory = None;
        SourceTable = ExampleLine;
        AutoSplitKey = true;
        DelayedInsert = true;
    
        layout
        {
            area(Content)
            {
                repeater(General)
                {
                    field("Document No."; Rec."Document No.")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Document No. field.';
                        Editable = false;
                    }
                    field("Line No."; Rec."Line No.")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Line No. field.';
                        Editable = false;
                    }
                    field("Example No."; Rec."Example No.")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Example No. field.';
                    }
                    field("Line Date"; Rec."Line Date")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Line Date field.';
                    }
                    field(Quantity; Rec.Quantity)
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Quantity field.';
                    }
                    field("Example Description"; Rec."Example Description")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Example Description field.';
                    }
                }
            }
        }
    }
    
  66. Create a new file named: ExampleDocumentList.Page.al.

  67. Create a new page in this file by using code snippets. Enter tpage and then press the Tab key.

  68. Change the ID to 50130 and the name to Example Document List.

  69. Set the SourceTable property to ExampleHeader and set the Caption property to Example Document List.

  70. Set the PageType property to List and set the UsageCategory property to Lists. Verify that the ApplicationArea property is set to All.

  71. Set the Editable property to false.

  72. Set the CardPageID property to Example Document.

  73. Verify that a content area is created in the layout section. If not, create an area called content.

  74. Remove the group in the area content and add a repeater with the name General.

  75. Add all fields within the repeater, except the No. Series field.

  76. Verify that the ApplicationArea property is set to All for all fields on the page.

  77. Add meaningful tooltips for all fields on the page.

  78. Remove the factboxes area.

  79. Remove the Actions area.

  80. Your code now resembles:

    page 50130 "Example Document List"
    {
        PageType = List;
        ApplicationArea = All;
        UsageCategory = Lists;
        SourceTable = ExampleHeader;
        Caption = 'Example Document List';
        Editable = false;
        CardPageId = "Example Document";
    
        layout
        {
            area(Content)
            {
                repeater(GroupName)
                {
                    field("No."; Rec."No.")
                    {
                        ToolTip = 'Specifies the value of the No. field.';
                    }
                    field("Document Date"; Rec."Document Date")
                    {
                        ToolTip = 'Specifies the value of the Document Date field.';
                    }
                    field("Posting Date"; Rec."Posting Date")
                    {
                        ToolTip = 'Specifies the value of the Posting Date field.';
                    }
                    field("No. Printed"; Rec."No. Printed")
                    {
                        ToolTip = 'Specifies the value of the No. Printed field.';
                    }
                }
            }
        }
    }
    
    
  81. Open the ExampleHeader.Table.al file and set the LookupPageID and DrillDownPageID properties to Example Document List.

  82. Open the ExampleDocument.Page.al file, and under the General group, add a part that is linked to the Example Line Subpage. Set the SubPageLink property.

    "Document No." = field("No.");

  83. Your code now resembles:

    page 50127 "Example Document"
    {
        PageType = Document;
        UsageCategory = Documents;
        SourceTable = ExampleHeader;
        Caption = 'Example Document';
    
        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("Document Date"; Rec."Document Date")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Document Date field.';
                    }
                    field("Posting Date"; Rec."Posting Date")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the Posting Date field.';
                    }
                    field("No. Printed"; Rec."No. Printed")
                    {
                        ApplicationArea = All;
                        ToolTip = 'Specifies the value of the No. Printed field.';
                    }
                }
                part(ExampleLineSubpage;"Example Line Subpage")
                {
                    SubPageLink = "Document No." = field("No.");
                          ApplicationArea = All;
                }
            }
        }
    }
    
  84. Open the launch.json file in the .vscode folder. Set the startupObjectId setting to 50130 and the startupObjectType to Page.

  85. Publish your extension to the sandbox. Select View > Command Palette... (Ctrl+Shift+P).

  86. Enter AL: Publish in the search box (or press the F5 key) and then select the command from the list.

  87. Verify that the Microsoft Dynamics 365 Business Central application launches and that the Example Header List page appears. Add new documents to the list.

  88. Test the No. Series functionality.

  89. For each new document, a new number should be generated automatically.