练习 - 使用数据操作语句

已完成

您是 CRONUS International Ltd. 的开发人员。您已了解如何在 AL 中使用数据操作语句,现在想要在开发环境中练习这项新技能。 您希望能够将总帐条目记录导出到特定表。 客户需要完成此任务和源代码,以便您可以按客户和源代码概要了解总金额。

任务

  • 创建新 AL 扩展。

  • 创建新表。

  • 创建链接到该表的页面。

  • 创建 codeunit 以将数据从不同的表复制到新创建的表。

步骤

  1. 在 Visual Studio Code 中,在您之前在此学习路径中创建的项目中创建一个名为 CustomerOverview 的新文件夹

  2. CustomerOverview 文件夹中创建后续步骤中提到的所有新的 .al 文件。

  3. 选择文件 > 新建文件,然后选择文件 > 保存以保存此文件。 将文件命名为 CustomerOverview.Table.al

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

  5. ID 更改为 50120,将名称更改为 Customer Overview

  6. DataClassification 属性设置为 CustomerContent,将 Caption 属性设置为 Customer Overview

  7. 删除 MyField 字段。

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

    字段编号 字段名称 数据类型 长度
    1 Entry No. Integer
    2 Customer No. Code 20
    3 Customer Name Text 100
    4 Source Code Code 20
    5 Amount Decimal
    6 LastRunDate DateTime
  9. 将表中每个字段的 DataClassification 属性设置为 CustomerContent

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

  11. 将主键设置为 Entry No.

  12. 删除全局变量部分。

  13. 删除表触发器。

  14. 您的文件现在类似此代码块:

    table 50120 "Customer Overview"
    {
        DataClassification = CustomerContent;
        Caption = 'Customer Overview';
    
        fields
        {
            field(1; "Entry No."; Integer)
            {
                DataClassification = CustomerContent;
                Caption = 'Entry No.';
            }
            field(2; "Customer No."; code[20])
            {
                DataClassification = CustomerContent;
                Caption = 'Customer No.';
            }
            field(3; "Customer Name"; Text[100])
            {
                DataClassification = CustomerContent;
                Caption = 'Customer Name';
            }
            field(4; "Source Code"; Code[20])
            {
                DataClassification = CustomerContent;
                Caption = 'Source Code';
            }
            field(5; "Amount"; Decimal)
            {
                DataClassification = CustomerContent;
                Caption = 'Amount';
            }
            field(6; "LastRunDate"; DateTime)
            {
                DataClassification = CustomerContent;
                Caption = 'LastRunDate';
            }
        }
    
        keys
        {
            key(Pk; "Entry No.")
            {
                Clustered = true;
            }
        }
    }
    
  15. 创建一个新文件,命名为 CustomerOverviewList.Page.al

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

  17. ID 更改为 50120,将名称更改为 Customer Overview List

  18. SourceTable 属性设置为 Customer Overview,将 Caption 属性设置为 Customer Overview List

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

  20. Editable 属性设置为 false

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

  22. 将该重复器的名称更改为 General

  23. 在重复器中,添加您之前创建的 Customer Overview 表的所有字段。

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

  25. 向页面中的所有字段添加有意义的工具提示。

  26. 删除 factboxes 区域。

  27. 删除操作区域。

  28. 您的文件现在类似此代码块:

    page 50120 "Customer Overview List"
    {
        PageType = List;
        ApplicationArea = All;
        UsageCategory = Lists;
        SourceTable = "Customer Overview";
        Caption = 'Customer Overview List';
        Editable = false;
    
        layout
        {
            area(Content)
            {
                repeater(General)
                {
    
                    field("Entry No."; Rec."Entry No.")
                    {
                        ToolTip = 'Specifies the value of the Entry No. field.';
                        ApplicationArea = All;
                    }
                    field("Customer No."; Rec."Customer No.")
                    {
                        ToolTip = 'Specifies the value of the Customer No. field.';
                        ApplicationArea = All;
                    }
                    field("Customer Name"; Rec."Customer Name")
                    {
                        ToolTip = 'Specifies the value of the Customer Name field.';
                        ApplicationArea = All;
                    }
                    field("Source Code"; Rec."Source Code")
                    {
                        ToolTip = 'Specifies the value of the Source Code field.';
                        ApplicationArea = All;
                    }
                    field(Amount; Rec.Amount)
                    {
                        ToolTip = 'Specifies the value of the Amount field.';
                        ApplicationArea = All;
                    }
                    field(LastRunDate; Rec.LastRunDate)
                    {
                        ToolTip = 'Specifies the value of the LastRunDate field.';
                        ApplicationArea = All;
                    }
                }
            }
        }
    }
    
  29. 创建一个新文件,命名为 CustomerOverviewMgmt.CodeUnit.al

  30. 使用代码片段在该文件中创建一个新的 codeunit。 输入 tcodeunit,然后按 Tab 键。

  31. ID 更改为 50120,将名称更改为 Customer Overview Mgmt

  32. 添加以下全局变量。

    名称 数据类型 子类型
    CustomerOverview 记录 Customer Overview
    Customer 记录 客户
    SourceCode 记录 源代码
    GLEntry 记录 总帐条目
    NextEntryNo Integer
  33. OnRun 触发器中,添加以下代码。

    Clear(SourceCode);
    Clear(CustomerOverview);
    Clear(GLEntry);
    
    if CustomerOverview.FindLast() then
        NextEntryNo := CustomerOverview."Entry No." + 1
    else
        NextEntryNo := 1;
    
    if SourceCode.FindSet() then
        repeat
            if Customer.FindSet() then
                repeat
                    GLEntry.SetRange("Source Type", GLEntry."Source Type"::Customer);
                    GLEntry.SetRange("Source Code", SourceCode.Code);
                    GLEntry.SetRange("Source No.", Customer."No.");
                    if GLEntry.FindSet() then begin
                        GLEntry.CalcSums(GLEntry.Amount);
                        CustomerOverview."Entry No." := NextEntryNo;
                        CustomerOverview."Customer No." := Customer."No.";
                        CustomerOverview."Customer Name" := Customer.Name;
                        CustomerOverview."Source Code" := SourceCode.Code;
                        CustomerOverview.Amount := GLEntry.Amount;
                        CustomerOverview.LastRunDate := CurrentDateTime();
                        CustomerOverview.Insert();
                        NextEntryNo += 1;
                    end;
                until Customer.Next() = 0;
        until SourceCode.Next() = 0
    
  34. 打开 CustomerOverviewList.Page.al 文件。

  35. 向页面添加 actions 区域。

  36. 在 actions 区域中添加 Processing 部分。

  37. Processing 部分中添加操作。

  38. 将操作的名称更改为 Import Records,然后对该操作设置以下属性:

    • Caption:Import Records

    • Image:Import

    • ApplicationArea:All

  39. OnAction 触发器中,创建一个局部变量。

    名称 数据类型 子类型
    UpdateCustomerOverview Codeunit Customer Overview Mgmt
  40. OnAction 触发器调用 codeunit RunTrigger

  41. 您的代码现在类似此代码块:

    page 50120 "Customer Overview List"
    {
        PageType = List;
        ApplicationArea = All;
        UsageCategory = Lists;
        SourceTable = "Customer Overview";
        Caption = 'Customer Overview List';
        Editable = false;
    
        layout
        {
            area(Content)
            {
                repeater(General)
                {
    
                    field("Entry No."; Rec."Entry No.")
                    {
                        ToolTip = 'Specifies the value of the Entry No. field.';
                        ApplicationArea = All;
                    }
                    field("Customer No."; Rec."Customer No.")
                    {
                        ToolTip = 'Specifies the value of the Customer No. field.';
                        ApplicationArea = All;
                    }
                    field("Customer Name"; Rec."Customer Name")
                    {
                        ToolTip = 'Specifies the value of the Customer Name field.';
                        ApplicationArea = All;
                    }
                    field("Source Code"; Rec."Source Code")
                    {
                        ToolTip = 'Specifies the value of the Source Code field.';
                        ApplicationArea = All;
                    }
                    field(Amount; Rec.Amount)
                    {
                        ToolTip = 'Specifies the value of the Amount field.';
                        ApplicationArea = All;
                    }
                    field(LastRunDate; Rec.LastRunDate)
                    {
                        ToolTip = 'Specifies the value of the LastRunDate field.';
                        ApplicationArea = All;
                    }
                }
            }
        }
        actions
        {
            area(Processing)
            {
                action("Import Records ")
                {
                    Caption = 'Import Records';
                    ToolTip = 'Import Records';
                    ApplicationArea = All;
                    Image = Import;
    
                    trigger OnAction()
                    var
                        CustomerOverviewMgmt: Codeunit "Customer Overview Mgmt";
                    begin
                        CustomerOverviewMgmt.Run();
                    end;
                }
            }
        }
    }
    
  42. 打开 .vscode 文件夹中的 launch.json 文件。 将 startupObjectId 设置为 50120,将 startupObjectType 设置为 Page

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

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

  45. 验证 Microsoft Dynamics 365 Business Central 应用程序是否启动以及 Customer Overview List 页是否显示。

  46. 通过选择导入记录操作来测试您的扩展。