Ejercicio: Usar instrucciones de manipulación de datos

Completado

Trabaja como desarrollador para CRONUS International Ltd. Ha aprendido a usar las instrucciones de manipulación de datos en AL y ahora quiere practicar con esta nueva capacidad en el entorno de desarrollo. Quiere poder exportar registros de contabilidad a una tabla específica. El cliente debe llevar a cabo esta tarea y código de origen para que usted tenga una visión general de los importes totales por cliente y código de origen.

Tareas

  • Crear una nueva extensión AL.

  • Crear una nueva tabla.

  • Crear una página que esté vinculada a la tabla.

  • Crear una codeunit para copiar datos de diferentes tablas a la tabla recién creada.

Pasos

  1. En Visual Studio Code, en el proyecto que ha creado antes en esta ruta de aprendizaje, cree una nueva carpeta llamada CustomerOverview.

  2. Cree todos los nuevos archivos .al mencionados en los siguientes pasos, en la carpeta CustomerOverview.

  3. Seleccione Archivo > Nuevo archivo y guarde este archivo seleccionando Archivo > Guardar. Ponga al archivo el nombre CustomerOverview.Table.al.

  4. Cree una nueva tabla en este archivo mediante el uso de fragmentos de código. Introduzca ttable y pulse la tecla Tab.

  5. Cambie el Id. a 50120 y el nombre a "Customer Overview".

  6. Establezca la propiedad DataClassification en CustomerContent y la propiedad Caption en Customer Overview.

  7. Quite el campo MyField.

  8. Cree los siguientes campos. Puede usar el fragmento tfield.

    N.° de campo Nombre de campo Tipo de datos Longitud
    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. Establezca la propiedad DataClassification de cada campo de la tabla en CustomerContent.

  10. Establezca la propiedad Caption de cada campo de la tabla.

  11. Establezca el campo Clave principal en Entry No.

  12. Elimine la sección de variables globales.

  13. Elimine los desencadenadores de tabla.

  14. El archivo debería parecerse a este bloque 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. Cree un nuevo archivo con el nombre CustomerOverviewList.Page.al.

  16. Cree una nueva página en este archivo mediante el uso de fragmentos de código. Introduzca tpage y pulse la tecla Tab.

  17. Cambie el Id. a 50120 y el nombre a Customer Overview List.

  18. Establezca la propiedad SourceTable en Customer Overview y la propiedad Caption en Customer Overview List.

  19. Establezca la propiedad PageType en List y la propiedad UsageCategory en Lists. Compruebe que la propiedad ApplicationArea esté establecida en All.

  20. Establezca la propiedad Editable en false.

  21. Compruebe que se haya creado un área content en la sección de diseño. De no ser así, cree un área denominada content.

  22. Cambie el nombre del repetidor a General.

  23. Agregue todos los campos de la tabla Customer Overview que creó anteriormente, dentro del repetidor.

  24. Compruebe que la propiedad ApplicationArea esté establecida en All para todos los campos de la página.

  25. Agregue información sobre herramientas significativa en todos los campos de la página.

  26. Elimine el área factboxes.

  27. Elimine el área actions.

  28. El archivo debería parecerse a este bloque 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. Cree un nuevo archivo con el nombre CustomerOverviewMgmt.CodeUnit.al.

  30. Cree una nueva codeunit en este archivo mediante el uso de fragmentos de código. Introduzca tcodeunit y pulse la tecla Tab.

  31. Cambie el Id. a 50120 y el nombre a Customer Overview Mgmt.

  32. Agregue las siguientes variables globales.

    Nombre Tipo de datos Subtipo
    CustomerOverview Registro Vista general de cliente
    Cliente Registro Cliente
    SourceCode Registro Source Code
    GLEntry Registro Movimiento de contabilidad
    NextEntryNo Integer
  33. En el desencadenador OnRun, agregue el siguiente 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 el archivo CustomerOverviewList.Page.al.

  35. Agregue un área actions en la página.

  36. En el área actions, agregue una sección Processing.

  37. En la sección Processing, agregue una acción.

  38. Cambie el nombre de la acción a Import Records y establezca las siguientes propiedades en esa acción:

    • Caption: Import Records

    • Image: Import

    • ApplicationArea: All

  39. En el desencadenador OnAction, cree una variable local.

    Nombre Tipo de datos Subtipo
    UpdateCustomerOverview Codeunit Customer Overview Mgmt
  40. Llame a la codeunit RunTrigger desde el desencadenador OnAction.

  41. El código debería parecerse a este bloque 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 el archivo launch.json, en la carpeta .vscode. Establezca la configuración startupObjectId en 50120 y la configuración startupObjectType en Page.

  43. Publique su extensión en el espacio aislado. Seleccione Ver > Paleta de comandos (Ctrl+Mayús+P).

  44. Introduzca AL: Publish en el cuadro de búsqueda (o presione la tecla F5) y, a continuación, seleccione el comando de la lista.

  45. Compruebe que se inicia la aplicación Microsoft Dynamics 365 Business Central y que aparece la página Customer Overview List.

  46. Pruebe la extensión seleccionando la acción Importar registros.