Examples of report extensions

Completed

The following examples explain how to create report extensions.

Example of adding fields to data items

This first example shows how to create a new report, CustomerSales, based on two tables: Customer and Cust. Ledger Entry.

The following example shows the AL code for the CustomerSales report:


report 50100 CustomerSales
{
    UsageCategory = ReportsAndAnalysis;
    ApplicationArea = All;
    Extensible = true;
    RDLCLayout = 'src/CustomerSales.rdl';

    dataset
    {
        dataitem(Customer; Customer)
        {
            column(No_Customer; "No.")
            {
                IncludeCaption = true;
            }
            column(Name_Customer; Name)
            {
                IncludeCaption = true;
            }
            dataitem(customerLedger; "Cust. Ledger Entry")
            {
                DataItemLink = "Customer No." = field("No.");

                column(CustomerNo_customerLedger; "Customer No.")
                {
                    IncludeCaption = true;
                }
                column(PostingDate_customerLedger; "Posting Date")
                {
                    IncludeCaption = true;
                }
                column(AmountLCY_customerLedger; "Amount (LCY)")
                {
                    IncludeCaption = true;
                }
            }
        }
    }
}

As the example shows, the CustomerSales report combines two tables, Customer and Cust. Ledger Entry, with a join on the customer number.

From the Customer table, the following fields are added in the dataset:

  • No.

  • Name

From the Cust. Ledger Entry table, the following fields are added in the dataset:

  • Customer No.

  • Posting Date

  • Amount (LCY)

In the layout of the report, a simple table was added that contains all fields.

Screenshot of the customer sales report layout.

Running the report will result in the following layout.

Screenshot of the customer ledger entry report in tabular format.

The next example demonstrates how to create a report extension that adds fields to the data items for the CustomerSales report.


reportextension 50100 CustomerSalesRepExt extends CustomerSales
{
    dataset
    {
        // Add changes to dataitems and columns here
        add(Customer)
        {
            column(City_Customer; City)
            {
                IncludeCaption = true;
            }
        }
        add(customerLedger)
        {
            column(Currency_Code;"Currency Code")
            {
                IncludeCaption = true;
            }
        }
    }
}

Copy the original layout of the CustomerSales report to the folder (project) of the report extension, rename it, add a reference in the report extension object, and then package (Ctrl + Shift + B) the project to update the layout.

Screenshot of the renamed reference in the report layout.

Now, the layout contains the new fields in the dataset, and you can add them in the table.

Screenshot of the layout of new fields in the dataset as they appear in the table.

After you deploy the report extension, open the Report Layout Selection page and change the built-in layout to the new layout of the report extension.

Screenshot of the Report Layout Selection page.

When you run the report, it should result in the following layout.

Screenshot of the updated report layout in tabular form.

Example of adding a new data item

This example shows how to add a data item to an existing report. The first task is to create a new report, CustomersAndVendors, with information from two tables: Customer and Vendor.


report 50101 CustomersAndVendors
{
    UsageCategory = ReportsAndAnalysis;
    ApplicationArea = All;
    RDLCLayout = 'src/CustomersAndVendors.rdl';

    dataset
    {
        dataitem(Customer; Customer)
        {
            column(dataitem_customer; 'dataitem_customer')
            { }
            column(No_Customer; "No.")
            { }
            column(Name_Customer; Name)
            { }
        }
        dataitem(Vendor; Vendor)
        {
            column(dataitem_vendor; 'dataitem_vendor')
            { }
            column(No_Vendor; "No.")
            { }
            column(Name_Vendor; Name)
            { }
        }
    }
}

The CustomersAndVendors report joins the Customer and Vendor table. Next, you can add a layout with a table that contains all columns, as shown in the following example.

Screenshot of the customers and vendors table layout.

The runtime result of the report is shown in the following screenshot.

Screenshot of the runtime result of the report.

Now, you can add a report extension with the following AL code:


reportextension 50101 CustomersAndVendorsRepExt extends CustomersAndVendors
{
    RDLCLayout = 'src/CustomersAndVendorsRepExt.rdl';
    dataset
    {
        // Add changes to dataitems and columns here
        add(Customer)
        {
            column(City_Customer; City)
            { }
        }
        add(Vendor)
        {
            column(City_Vendor; City)
            { }
        }
        addlast(Vendor)
        {
            dataitem(Contact; Contact)
            {
                column(dataitem_contact; 'dataitem_contact')
                { }
                column(No_Contact; "No.")
                { }
                column(Name_Contact; Name)
                { }
                column(City_Contact; City)
                { }
            }
        }
    }
}

The CustomersAndVendorsRepExt report extension will add the City field to the Customer and to the Vendor data item, and it will also add a new Contact data item to the Vendor data item.

Design the layout of the CustomersAndVendorsRepExt report extension and add all columns of the dataset into a table, as shown in the following example.

Screenshot of the customers and vendors report extension layout.

Then, when you run the report, you should have the following result.

Screenshot of the customers and vendors report extension results in a table.

The columns of the Contact data item are added to the Vendor columns in the dataset, and the Vendor rows are repeated for all contacts. Though this action creates a real dataset, it might not be the expected or desired result. As such, you should be careful when adding a data item to an existing report.

Example of adding a field to a report and request page

The following example demonstrates how to add a field to the dataset of the Standard Sales - Invoice report.

The following report extension adds the Order No. field to the Standard Sales - Invoice report. It also declares a new variable, DisplayOrderInfo, which is added to the request page and dataset.

reportextension 50102 StandardSalesInvoiceRepExt extends "Standard Sales - Invoice"
{
    RDLCLayout = './src/StandardSalesInvoiceExtended.rdlc';

    dataset
    {
        // Add changes to dataitems and columns here
        add(Line)
        {
            column(Order_No_Line_Lbl; Line.fieldCaption("Order No."))
            {}
            column(Order_No_Line; Line."Order No.")
            {}
            column(DisplayOrderInfo;DisplayOrderInfo)
            {}
        }
    }

    requestpage
    {
        // Add changes to the requestpage here
        layout
        {
            addlast(Options)
            {
                field(DisplayOrderInfo; DisplayOrderInfo)
                {
                    ApplicationArea = Basic, Suite;
                    Caption = 'Show Order Information';
                    ToolTip = 'Specifies if you want Order Information to be shown on the document.';
                }
            }
        }
    }
    var
        DisplayOrderInfo: Boolean;
}

In the report layout, the Order No. field (and label) is added to the details table, as shown in the following screenshot.

Screenshot of the Order Number field added to the report layout.

Furthermore, the Row Visibility of the new details row in the table contains the following expression.

Screenshot of the new details row and row visibility expressions in the table.

Now, when you run the Standard Sales - Invoice report with the new layout, the request page should appear as shown in the following screenshot.

Screenshot of the Standard Sales Invoice report layout.

In the report, the Order No. field is visible when it contains data.

Screenshot of data contained in the Order Number field in the Standard Sales invoice report.