Dynamics 365 Business Central: How to set the default report layout via AL (“Report Layout List 2000000234”)

Dynamics 365 Business Central

Hi, Readers.
We discussed how to specify the report layout before printing via AL (“Report Layout List 2000000234” not “Custom Report Layout 9650”) last week, and today I want to continue this discussion, how to specify the default report layout via AL (“Report Layout List 2000000234”).

As you might know, Microsoft released a new page for report layout administration in 2022 release wave 1 (BC20), more details: New pages for report layout administration (page 9660 “Report Layouts”)

And Microsoft is deprecating Report Layout Selection (9652, List) page.
More info:
1. (Legacy) Create and Modify Custom Report Layouts:

Custom report layouts is a legacy feature that is being phased out. Instead, you should start creating user-defined layouts as described here.

2. Setting the Layout Used by a Report:

You can’t use the Report Layouts page for Word and RDLC layouts that were created by using the legacy Custom Layouts feature. In fact, you won’t even see these custom layouts listed on the Report Layouts page. For these layouts, you can only set them by using Report Layout Selection page.

On the Report Layouts page, we can set the current layout as the default layout for the specified report.

“./SalesReceivables/Document/StandardSalesOrderConf.rdlc” has been set as the default layout for Report “Sales – Confirmation”

The question is, can this process be done through customization? Yes, it’s possible, but it’s a bit complicated.

First of all, the Report Layouts app is not included in the base application, you need to add a dependencies setting in app.json file to download the symbol file.
ReportLayouts:

  "dependencies": [
    {
      "id": "b7174aae-753c-4e71-bacb-d973995dce5e",
      "name": "_Exclude_ReportLayouts",
      "publisher": "Microsoft",
      "version": "22.0.0.0"
    }
  ]

Standard objects in ReportLayouts:

Then we can find some clues in page 9660 “Report Layouts” and codeunit 9660 “Report Layouts Impl.”.

But the Access property of codeunit 9660 “Report Layouts Impl.” is Internal, and the SetDefaultReportLayoutSelection method is also an internal procedure. We cannot use this standard method directly.

I tried to recreate the method with reference to the standard code, and this works😁.

To make it easier to understand, let me make a simple example similar to last week’s.
Before opening the Request Page of the report, the user can specify the report layout on the order, and the layout selected on the order will be set as the default layout.

Test video:

Not very difficult, give it a try!!!😁

Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)

tableextension 50111 SalesHeaderExt extends "Sales Header"
{
    fields
    {
        field(50100; "Report Name"; Text[250])
        {
            DataClassification = CustomerContent;
            Caption = 'Report Name';
            trigger OnLookup()
            var
                ReportLayoutList: Record "Report Layout List";
            begin
                ReportLayoutList.Reset();
                ReportLayoutList.SetRange("Report ID", 1305);
                if Page.RunModal(Page::"Report Layouts", ReportLayoutList) = Action::LookupOK then begin
                    SetDefaultReportLayoutSelection(ReportLayoutList);
                    Rec."Report Name" := ReportLayoutList.Caption;
                    Rec.Modify(true);
                end;
            end;
        }
    }

    var
        TenantReportLayoutSelection: Record "Tenant Report Layout Selection";
        EmptyGuid: Guid;
        DefaultLayoutSetTxt: Label '"%1" has been set as the default layout for Report "%2"', Comment = '%1 = Layout Name, %2 = Report Name';

    procedure SetDefaultReportLayoutSelection(SelectedReportLayoutList: Record "Report Layout List")
    var
        ReportLayoutSelection: Record "Report Layout Selection";
    begin
        // Add to TenantReportLayoutSelection table with an Empty Guid.
        AddLayoutSelection(SelectedReportLayoutList, EmptyGuid);
        // Add to the report layout selection table
        if ReportLayoutSelection.get(SelectedReportLayoutList."Report ID", CompanyName) then begin
            ReportLayoutSelection.Type := GetReportLayoutSelectionCorrespondingEnum(SelectedReportLayoutList);
            ReportLayoutSelection.Modify(true);
        end else begin
            ReportLayoutSelection."Report ID" := SelectedReportLayoutList."Report ID";
            ReportLayoutSelection."Company Name" := CompanyName;
            ReportLayoutSelection."Custom Report Layout Code" := '';
            ReportLayoutSelection.Type := GetReportLayoutSelectionCorrespondingEnum(SelectedReportLayoutList);
            ReportLayoutSelection.Insert(true);
        end;
        Message(DefaultLayoutSetTxt, SelectedReportLayoutList."Caption", SelectedReportLayoutList."Report Name");
    end;

    local procedure AddLayoutSelection(SelectedReportLayoutList: Record "Report Layout List"; UserId: Guid): Boolean
    begin
        TenantReportLayoutSelection.Init();
        TenantReportLayoutSelection."App ID" := SelectedReportLayoutList."Application ID";
        TenantReportLayoutSelection."Company Name" := CompanyName;
        TenantReportLayoutSelection."Layout Name" := SelectedReportLayoutList."Name";
        TenantReportLayoutSelection."Report ID" := SelectedReportLayoutList."Report ID";
        TenantReportLayoutSelection."User ID" := UserId;
        if not TenantReportLayoutSelection.Insert(true) then
            TenantReportLayoutSelection.Modify(true);
    end;

    local procedure GetReportLayoutSelectionCorrespondingEnum(SelectedReportLayoutList: Record "Report Layout List"): Integer
    begin
        case SelectedReportLayoutList."Layout Format" of
            SelectedReportLayoutList."Layout Format"::RDLC:
                exit(0);
            SelectedReportLayoutList."Layout Format"::Word:
                exit(1);
            SelectedReportLayoutList."Layout Format"::Excel:
                exit(3);
            SelectedReportLayoutList."Layout Format"::Custom:
                exit(4);
        end
    end;
}
pageextension 50112 SalesOrderExt extends "Sales Order"
{
    layout
    {
        addafter("Sell-to Customer Name")
        {
            field("Report Name"; Rec."Report Name")
            {
                ApplicationArea = All;
                Caption = 'Report Name';
                ToolTip = 'Report Name';
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL