Report.TargetFormat() Method in Business Central 2025 Wave 1

With the release of Microsoft Dynamics 365 Business Central 2025 Wave 1, Microsoft has continued to improve the AL language and reporting capabilities. One notable addition for developers working with reports is the new Report.TargetFormat() method. This small yet powerful enhancement gives developers more control and visibility over how reports are executed, especially in scenarios involving automation, scheduling, or integration.

The Report.TargetFormat() method allows you to set the target output format for a report at runtime. This is particularly useful in scenarios where the desired output format isn’t static or needs to be determined based on user preferences, system configurations, or specific business logic.

Imagine you have a page action that allows users to export a list of customers. Instead of having separate actions for different formats, you can use Report.TargetFormat() to dynamically generate the output in Excel.

pageextension 50100 CustomerListExt extends "Customer List"
{
    actions
    {
        addfirst(Processing)
        {
            action(ExportToExcel)
            {
                Caption = 'Export to Excel';
                Image = ExportExcel;
                trigger OnAction()
                var
                    CustomerListReport: Report "Customer List";
                    ReportSettings: Record "Report Settings";
                begin
                    ReportSettings.TargetFormat := Enum::"Report Format"::Excel;
                    CustomerListReport.Run(ReportSettings);
                end;
            }
        }
    }
}

In this example, before running the “Customer List” report, we set the TargetFormat property of a ReportSettings record to Excel. When the report is executed using Run(ReportSettings), it will automatically be generated as an Excel file.

Consider a scenario where you need to automatically generate and archive a sales order confirmation as a PDF after the order is posted. You can achieve this within a codeunit.

codeunit 50101 SalesOrderProcessing
{
    procedure GenerateConfirmationPDF(SalesHeaderRec: Record "Sales Header")
    var
        SalesConfirmationReport: Report "Sales - Order Conf.";
        ReportSettings: Record "Report Settings";
    begin
        ReportSettings.TargetFormat := Enum::"Report Format"::PDF;
        SalesConfirmationReport.SetTableView(SalesHeaderRec);
        SalesConfirmationReport.Run(ReportSettings);
        // Code to archive the generated PDF can be added here
    end;
}

Here, within the GenerateConfirmationPDF procedure, we explicitly set the TargetFormat to PDF for the “Sales – Order Conf.” report. When the report is run, it will be generated directly as a PDF document.

You can create a more user-friendly experience by presenting users with a choice of output formats before running a report.

pageextension 50102 ItemListExt extends "Item List"
{
    actions
    {
        addfirst(Processing)
        {
            action(ExportItemData)
            {
                Caption = 'Export Item Data';
                Image = Export;
                trigger OnAction()
                var
                    ItemListReport: Report "Item List";
                    ReportSettings: Record "Report Settings";
                    ExportFormat: Option "PDF","Excel","CSV";
                begin
                    ExportFormat := Dialog.Confirm('Choose Export Format:', true, ExportFormat);
                    case ExportFormat of
                        ExportFormat::PDF:
                            ReportSettings.TargetFormat := Enum::"Report Format"::PDF;
                        ExportFormat::Excel:
                            ReportSettings.TargetFormat := Enum::"Report Format"::Excel;
                        ExportFormat::CSV:
                            ReportSettings.TargetFormat := Enum::"Report Format"::CSV;
                    end;
                    ItemListReport.Run(ReportSettings);
                end;
            }
        }
    }
}

The Report.TargetFormat() method is a subtle yet powerful addition to the AL reporting toolbox in Business Central 2025 Wave 1. It unlocks a new level of flexibility for report behavior and presentation, enabling developers to create more intelligent, format-aware reporting solutions.

Stay tuned for more.

How to export data using TextBuilder in Business central.

Today we will see how we can data from business central table using new data type Text Builder.

To check how this data type work build small program as below which pick up data from customer table and export the data in text file.

pageextension 50101 CustomerMasterExtension extends "Customer List"
{
    actions
    {
        addlast(General)
        {
            action(DataExport)
            {
                Caption='Data Export to Text';
                ApplicationArea=All;
                PromotedCategory=Process;
                Promoted=true;
                Image=ExportDatabase;
                trigger OnAction()
                var
                CustomerMaster :Record Customer;
                Tempblob:Codeunit "Temp Blob";
                TextFileBuilder :TextBuilder;
                FileName:Text;
                InStreamData:InStream;
                OutStreamData:OutStream;
                begin 
                    FileName:='CustomerData.Txt';
                    TextFileBuilder.AppendLine('Customer No'+','+'Customer Name'+','+'Balance');
                    If CustomerMaster.FindSet() then repeat
                        CustomerMaster.SetAutoCalcFields(CustomerMaster.Balance);
                        TextFileBuilder.AppendLine(CustomerMaster."No."+','+CustomerMaster.Name+','+Format(CustomerMaster.Balance));
                    until CustomerMaster.Next()=0;
                    Tempblob.CreateOutStream(OutStreamData);
                    OutStreamData.WriteText(TextFileBuilder.ToText());
                    Tempblob.CreateInStream(InStreamData);
                    DownloadFromStream(InStreamData,'','','',FileName);
                end;
            }
        }
    }
}

If you look at the above code it is taking required data from customer table and export the data in text file as below.

To get more insight of data type have a look at MS documentation from below link

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/textbuilder/textbuilder-data-type

Hope this will help you.

Stay tuned for more.