Exercício – Usar instruções para manipulação de dados

Concluído

Você é desenvolvedor da CRONUS International Ltd. Você aprendeu a usar instruções de manipulação de dados na AL e agora deseja praticar essa nova habilidade no ambiente de desenvolvimento. Você quer poder exportar os registros de contabilidade/entrada para uma tabela específica. O cliente precisa concluir esta tarefa e o código-fonte para que você tenha uma visão geral dos valores totais por cliente e código-fonte.

Tarefas

  • Criar uma nova extensão AL.

  • Crie uma nova tabela.

  • Crie uma página vinculada à tabela.

  • Crie um codeunit para copiar os dados de tabelas diferentes para a tabela recém-criada.

Etapas

  1. No Visual Studio Code, no projeto que você criou anteriormente neste roteiro de aprendizagem, crie uma nova pasta chamada: CustomerOverview

  2. Crie todos os novos arquivos .al mencionados nas próximas etapas, na pasta CustomerOverview.

  3. Selecione Arquivo > Novo Arquivo e salve o arquivo escolhendo Arquivo > Salvar. Nomeie o arquivo como CustomerOverview.Table.al.

  4. Crie uma nova tabela no arquivo usando trechos de código. Insira ttable e pressione a tecla Tab.

  5. Altere a ID para 50120 e o nome para "Customer Overview".

  6. Defina a propriedade DataClassification como CustomerContent e defina a propriedade Caption como Customer Overview.

  7. Remova o campo MyField.

  8. Crie os campos abaixo. Você pode usar o trecho tfield.

    Nº do campo Nome do campo Tipo de dados Comprimento
    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. Defina a propriedade DataClassification para cada campo da tabela como CustomerContent.

  10. Defina a propriedade Caption para cada campo da tabela.

  11. Defina o campo Chave Primária Nº de entrada

  12. Remova a seção de variáveis globais.

  13. Remova os gatilhos da tabela.

  14. Agora o arquivo é semelhante a este bloco de código:

    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. Crie um novo arquivo, chamado CustomerOverviewList.Page.al.

  16. Crie uma nova página no arquivo usando trechos de código. Insira tpage e pressione a tecla Tab.

  17. Altere a ID para 50120 e o name para Customer Overview List.

  18. Defina a propriedade SourceTable como Customer Overview e defina a propriedade Caption como Customer Overview List.

  19. Defina a propriedade PageType como List e defina a propriedade UsageCategory como Lists. Verifique se a propriedade ApplicationArea está definida como All.

  20. Defina a propriedade Editable como false.

  21. Verifique se uma área content foi criada na seção de layout. Em caso negativo, crie uma área content.

  22. Altere o nome do repetidor para General.

  23. Adicione todos os campos da tabela Customer Overview que você criou anteriormente, no repetidor.

  24. Verifique se a propriedade ApplicationArea está definida como All para todos os campos da página.

  25. Adicione dicas de ferramentas significativas a todos os campos da página.

  26. Remova a área de factboxes.

  27. Remova a área de ações.

  28. Agora o arquivo é semelhante a este bloco de código:

    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. Crie um novo arquivo, denominado CustomerOverviewMgmt.CodeUnit.al.

  30. Crie uma nova codeunit nesse arquivo usando trechos de código. Insira tcodeunit e pressione a tecla Tab.

  31. Altere a ID para 50120 e o name para Customer Overview Mgmt.

  32. Adicione as seguintes variáveis globais.

    Nome DataType Subtipo
    CustomerOverview Registro Customer Overview
    Cliente Registro Cliente
    SourceCode Registro Código-fonte
    GLEntry Registro Entrada de Contabilidade
    NextEntryNo Integer
  33. No gatilho OnRun, adicione o seguinte código.

    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. Abra o arquivo CustomerOverviewList.Page.al.

  35. Adicione uma área actions à página.

  36. Na área actions, adicione uma seção Processing.

  37. Na seção Processing, adicione uma ação.

  38. Altere o nome da ação para Import Records e defina as propriedades a seguir nesta ação:

    • Legenda: Import Records

    • Image: Import

    • ApplicationArea: All

  39. No gatilho OnAction, crie uma variável local.

    Nome DataType Subtipo
    UpdateCustomerOverview Codeunit Customer Overview Mgmt
  40. Chame o codeunit RunTrigger do gatilho OnAction.

  41. Agora o código é semelhante a este bloco de código:

    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. Abra o arquivo launch.json na pasta .vscode. Defina a configuração startupObjectId como 50120 e a configuração startupObjectType como Page.

  43. Publique sua extensão na área restrita. Selecione Exibir > Paleta de Comandos... (Ctrl+Shift+P).

  44. Insira AL: Publish na caixa de pesquisa (ou pressione a tecla F5) e selecione o comando na lista.

  45. Verifique se o aplicativo Microsoft Dynamics 365 Business Central é iniciado e se a página Lista de Visão Geral do Cliente é exibida.

  46. Teste sua extensão selecionando a ação Importar Registros.