练习 - 添加主数据的表和页面

已完成

应用场景

您是 CRONUS International Ltd. 的一位开发人员。公司要求您对示例设置扩展进行延展。 要求是添加相应的主表和补充表,以及所有适当的页面和设计模式。

任务

  • 示例应用程序扩展添加两个新表。

  • ExampleExample Type 表创建页面。

  • 应用编号 系列设计模式。

步骤

  1. 在 VS Code 中,在您的扩展中添加一个新文件夹,命名为:MasterData。 所有新文件都将添加到这个新文件夹中。

  2. 创建一个新文件,命名为:Example.Table.al

  3. 使用代码片段在此文件中创建一个新表。 输入 ttable,然后按 Tab 键。

  4. ID 更改为 50125,将 name 更改为 Example

  5. DataClassification 属性设置为 CustomerContent,并将 Caption 属性设置为 Example

  6. 删除我的字段字段。

  7. 创建以下字段。 您可以使用 tfield 代码片段。

    字段编号 字段名称 数据类型 长度
    1 编号 代码 20
    2 说明 文本 50
    3 示例类型代码 代码 10
    4 编号 系列 代码 20
  8. 将表中每个字段的 DataClassification 属性设置为 CustomerContent

  9. 设置表中每个字段的 Caption 属性。

  10. 将 Primary Key 设置为 No.

  11. 删除 global var 部分。

  12. 删除表触发器。

  13. 您的代码现在应如下所示:

     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;
             }
         }
     }
    
  14. 创建一个新文件,命名为:ExampleType.Table.al

  15. 使用代码片段在此文件中创建一个新表。 输入 ttable,然后按 Tab 键。

  16. ID 更改为 50126,将 name 更改为 ExampleType

  17. DataClassification 属性设置为 CustomerContent,将 Caption 属性设置为 Example Type

  18. 删除我的字段字段。

  19. 创建以下字段。 您可以使用 tfield 代码片段。

    字段编号 字段名称 数据类型 长度
    1 代码 代码 10
    2 说明 文本 50
  20. 将表中每个字段的 DataClassification 属性设置为 CustomerContent

  21. 设置表中每个字段的 Caption 属性。

  22. 将 Primary Key 设置为 Code

  23. 删除 global var 部分。

  24. 删除表触发器。

  25. 您的代码现在应如下所示:

     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;
             }
         }
     }
    
  26. 打开 Example.Table.al 文件,并将 Example Type Code 字段的 TableRelation 属性设置为 Example Type 表。

     field(3; "Example Type Code"; Code[10])
             {
                 DataClassification = CustomerContent;
                 Caption = 'Example Type Code';
                 TableRelation = ExampleType;
             }
    
  27. 如果您的项目中没有名为 Example Setup 的表,请创建一个。 (文件名:ExampleSetup.Table.al)

  28. 代码应如下所示:

    
     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;
             }
         }
     }
    
  29. 如果您的项目中没有 Example Setup Card 页面,请创建一个(文件名: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;
    }
    
  30. Example 表对象中创建两个全局变量。

    
     var
         NoSeriesManagement: Codeunit NoSeriesManagement;
         ExampleSetup: Record "Example Setup";
    
  31. Example 表的 OnInsert 触发器中编写代码以初始化编号系列。

    
     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;
    
  32. 创建 AssistEdit 触发器并写入代码以启用通过使用编号系列选择。

     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;
    
  33. “Example”表现在应该如下所示:

     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;
     }
    
  34. 创建一个新文件,命名为:ExampleCard.Page.al

  35. 使用代码片段在此文件中创建一个新页面。 输入 tpage,然后按 Tab 键。

  36. ID 更改为 50125,将 name 更改为 Example Card

  37. SourceTable 属性设置为 Example,将 Caption 属性设置为 Example Card

  38. PageType 属性设置为,将 UsageCategory 属性设置为。 删除 ApplicationArea 属性。

  39. 验证是否在布局部分创建了 content 区域。 如果没有,请创建一个名为 content 的区域。

  40. 将第一个组的名称更改为常规。 将组的 Caption 属性设置为常规

  41. 将所有字段添加到常规组,但 No. Series 字段除外。

  42. 验证页面上所有字段的 ApplicationArea 属性是否均已设置为 All

  43. 为每个字段添加有意义的工具提示。

  44. 为“No.”字段添加一个 OnAssistEdit 触发器。

  45. No. 字段的 OnAssistEdit 触发器中,编写代码,在表级别运行 AssistEdit 函数。

     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.';
                     }
                 }
             }
         }
     }
    
  46. 添加一个新文件,命名为:ExampleList.Page.al

  47. 使用代码片段在此文件中创建一个新页面。 输入 tpage,然后按 Tab 键。

  48. ID 更改为 50126,将 name 更改为 Example List

  49. SourceTable 属性设置为Example,将 Caption 属性设置为 Example List

  50. PageType 属性设置为 List,将 UsageCategory 属性设置为 Lists。 确保 ApplicationArea 属性设置为 All

  51. Editable 属性设置为 false,并将 CardPageID 属性设置为 Example Card

  52. 验证是否在布局部分创建了 content 区域。 如果没有,请创建一个名为 content 的区域。

  53. 确保有一个名为 General 的重复器。

  54. 添加重复器中的所有字段,但 No. Series 字段除外。

  55. 验证页面上所有字段的 ApplicationArea 属性是否均已设置为 All

  56. 为页面中的每个字段添加有意义的工具提示。

  57. 从页面中删除 actions 区域。

  58. 从页面中删除 factbox 区域。

  59. 该代码现在应如下所示:

     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.';
                     }
                 }
             }
         }
     }
    
  60. 添加一个新文件,命名为:ExampleTypes.Page.al

  61. 使用代码片段在此文件中创建一个新页面。 输入 tpage,然后按 Tab 键。

  62. ID 更改为 50124,将 name 更改为 Example Types

  63. SourceTable 属性设置为 Example Type,并将 Caption 属性设置为Example Types

  64. PageType 属性设置为 List,将 UsageCategory 属性设置为 Lists。 确保 ApplicationArea 属性设置为 All

  65. Editable 属性设置为 true

  66. 验证是否在布局部分创建了 content 区域。 如果没有,请创建一个名为 content 的区域。

  67. 确保有一个名为 General 的重复器。

  68. 在重复器中添加所有字段。

  69. 验证页面上所有字段的 ApplicationArea 属性是否均已设置为 All

  70. 为所有字段添加有意义的工具提示。

  71. 从页面中删除 actions 区域。

  72. 从页面中删除 factbox 区域。

  73. 该代码现在如下所示:

     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.';
                     }
                 }
             }
         }
     }
    
  74. 打开 Example.Table.al 文件,并将 LookupPageIDDrillDownPageID 属性设置为 Example List

  75. 打开 ExampleType.Table.al 文件,并将 LookupPageIDDrillDownPageID 属性设置为 Example Types

  76. 打开 Example List 页面,并将 CardPageId 属性设置为 Example Card,将 Editable 设置为 false

  77. 打开 .vscode 文件夹中的 launch.json 文件。 将 startupObjectId 设置为 50126,将 startupObjectType 设置为 Page

  78. 将扩展发布到沙盒。 选择视图 > 命令面板... (Ctrl+Shift+P)。

  79. 在搜索框中输入 AL: 发布(或者按 F5 键),然后从列表中选择命令。

  80. 验证 Dynamics 365 Business Central 应用程序是否启动以及 Example List 页是否显示。 如果要测试扩展,您可以使用 Example Card 将新示例添加到列表中并添加 Example Types

  81. 您还可以测试 No. Series 功能。 如果您在 Example Setup Card 中为示例设置了一个编号系列,则对于您添加的每个新示例,应自动生成一个新编号。