How to Manually Change a Report Layout Before Rendering It in Dynamics 365 Business Central
Introduction
Microsoft Dynamics 365 Business Central lets a single report carry many layouts. A user normally picks one from the Report Layout selector on the request page, or an administrator sets a per-company default on the Report Layout Selection page. But there is a third scenario that neither of those covers well: when your AL code should decide which layout to render, for one specific run, without the user choosing every time.
This comes up constantly. You might want a dedicated “Print – Finance Audit” button that always prints the internal copy; or you might want the layout picked automatically from the record – a foreign-currency invoice gets the financial summary, a warehouse copy gets the logistics layout – so the user just presses Print and the right document comes out.
This article shows how to set the layout in AL immediately before the report renders, using the platform codeunit “Design-time Report Selection” and its SetSelectedLayout procedure. We build it on the posted Sales Invoice – the standard report “Standard Sales – Invoice” (report 1306) – which we have already extended with four selectable Word layouts. Two patterns are covered: selecting a layout when your own code launches the report, and intercepting the built-in Print action to inject a layout through the OnAfterSubstituteReport event.
Prerequisites
- Access to a Dynamics 365 Business Central instance (SaaS or on-premises; a sandbox is recommended for testing).
- Business Central 2022 release wave 1 (v20) or later – modern report layouts and layouts added through a report extension both require this baseline.
- Basic knowledge of AL (Application Language) code.
- Visual Studio Code with the AL Language extension installed.
- A report that has more than one layout registered against it. Here we use a report extension that adds four Word layouts to “Standard Sales – Invoice”.
Requirement
Out of the box, Business Central already gives users and administrators two ways to choose a layout:
- At run time: once a report has more than one layout, the request page (and the Send-to dialog) shows a Report Layout selector, and the user picks which layout to use for that run.
- As a default: the Report Layout Selection page sets, per company, which layout a report uses by default. This is documented on Microsoft Learn under “Setting the Report Layout”.
What neither of those does is let code make the choice for a single run. That is the gap we close here. The supported tool for it is the base-application codeunit “Design-time Report Selection” (codeunit 9654). Its key procedure – documented on Microsoft Learn – is:
procedure SetSelectedLayout(LayoutName: Text[250])
procedure SetSelectedLayout(LayoutName: Text[250]; AppID: Guid)
procedure GetSelectedLayout(): Text[250]
procedure ClearLayoutSelection()
Calling SetSelectedLayout before a report runs tells the platform which registered layout to use for the next render in the session. The single-parameter overload is enough when the layout lives in your own (or the current) extension; pass the AppID overload when the layout belongs to a different app. The important rule: the LayoutName you pass must exactly match the name of a layout registered against that report – so the first thing to get right is the layout registration itself.
Solution (Implementation Steps)
The build has three moving parts: the layouts we want to choose between, a direct pattern for when our code launches the report, and an interception pattern for the built-in Print action. We take them in order.
Step 1: Register the layouts you want to choose between
Layout selection can only pick from layouts the report actually knows about. We register four Word layouts on the standard report through a report extension, in its rendering section. The layout name in each layout(…) block – not the Caption – is the string SetSelectedLayout expects later, so note those names now.
reportextension 50101 “INK Sales Invoice Layouts” extends “Standard Sales – Invoice”
{
dataset
{
// … columns for cost, weight, tracking, dimensions, totals, and
// visibility flags are added here (see the full source) …
}
rendering
{
layout(“PSI Standard Customer”)
{
Type = Word;
LayoutFile = ‘./SRC/Layouts/PSI_StandardCustomer.docx’;
Caption = ‘Posted Sales Invoice – Standard Customer’;
Summary = ‘Customer-facing standard invoice.’;
}
layout(“PSI Financial Summary”)
{
Type = Word;
LayoutFile = ‘./SRC/Layouts/PSI_FinancialSummary.docx’;
Caption = ‘Posted Sales Invoice – Financial Summary’;
Summary = ‘Financial breakdown with sales summary and outstanding balance.’;
}
layout(“PSI Warehouse Shipping”)
{
Type = Word;
LayoutFile = ‘./SRC/Layouts/PSI_WarehouseShipping.docx’;
Caption = ‘Posted Sales Invoice – Warehouse & Shipping’;
Summary = ‘Logistics copy: weights, volumes, packages, item tracking.’;
}
layout(“PSI Finance Audit”)
{
Type = Word;
LayoutFile = ‘./SRC/Layouts/PSI_FinanceAudit.docx’;
Caption = ‘Posted Sales Invoice – Finance Audit (Internal)’;
Summary = ‘Internal accounting/audit copy. Not for customers.’;
}
}
}
The four layout names here are PSI Standard Customer, PSI Financial Summary, PSI Warehouse Shipping, and PSI Finance Audit. The dataset itself is widened elsewhere in the same extension so every field and total each layout shows already exists as a column; changing the layout never changes the data, only how it is presented.
Step 2: Approach A – select the layout when your code launches the report
This is the cleanest pattern, and it is the one to reach for whenever you own the launch – for example, your own action buttons. You set the layout, then run the report yourself. Because your code runs the report on the very next line, the selection is in effect for exactly that run.
pageextension 60100 “INK Posted Sales Invoice Ext.” extends “Posted Sales Invoice”
{
actions
{
addlast(processing)
{
action(FinanceAudit)
{
Caption = ‘Print – Finance Audit’;
ApplicationArea = All;
Image = PrintReport;
ToolTip = ‘Print this invoice using the internal Finance Audit layout.’;
Promoted = true;
PromotedCategory = Category6;
trigger OnAction()
begin
PrintWithLayout(LayoutFinanceAuditLbl);
end;
}
// FinanceSummary and WarehouseShipping actions follow the same
// pattern, each passing its own layout label to PrintWithLayout.
}
}
local procedure PrintWithLayout(LayoutCaption: Text)
var
SalesInvHeader: Record “Sales Invoice Header”;
DesigntimeReportSelection: Codeunit “Design-time Report Selection”;
begin
SalesInvHeader := Rec;
SalesInvHeader.SetRecFilter();
DesigntimeReportSelection.SetSelectedLayout(LayoutCaption);
Report.RunModal(Report::”Standard Sales – Invoice”, true, false, SalesInvHeader);
end;
var
LayoutStandardCustomerLbl: Label ‘PSI Standard Customer’, Locked = true;
LayoutFinancialSummaryLbl: Label ‘PSI Financial Summary’, Locked = true;
LayoutWarehouseShippingLbl: Label ‘PSI Warehouse Shipping’, Locked = true;
LayoutFinanceAuditLbl: Label ‘PSI Finance Audit’, Locked = true;
}
Two details matter. First, the label values (‘PSI Standard Customer’, and so on) must equal the layout names from the rendering section in Step 1, not their captions – and marking the labels Locked = true keeps them stable so translation can never break the match. Second, SetSelectedLayout is called right before Report.RunModal, which runs “Standard Sales – Invoice” against the single filtered record. Add one promoted button per layout, each calling PrintWithLayout with its own label, and the user gets one-click access to a specific layout with no selector to navigate.
Step 3: Approach B – intercept the built-in Print action
Sometimes you do not launch the report yourself – you want the page’s existing Print action to keep working, but to honour a layout your code decides. The standard Print action runs the report through the platform’s own report-resolution plumbing, so instead of calling SetSelectedLayout inline, we split the job in two: capture the user’s choice when the action starts, then apply that choice at the exact point the platform resolves the report.
On the page extension we modify the Print action and use its OnBeforeAction trigger to ask which layout to print and stash the answer in a single-instance codeunit:
modify(Print)
{
trigger OnBeforeAction()
var
LayoutSelection: Codeunit “INK Layout Selection”;
Selection: Integer;
begin
Selection := Dialog.StrMenu(LayoutOptionsLbl, 1, SelectLayoutInstrLbl);
case Selection of
2:
LayoutSelection.SetLayout(LayoutFinancialSummaryLbl);
3:
LayoutSelection.SetLayout(LayoutWarehouseShippingLbl);
4:
LayoutSelection.SetLayout(LayoutFinanceAuditLbl);
else
LayoutSelection.SetLayout(LayoutStandardCustomerLbl);
end;
end;
}
var
LayoutOptionsLbl: Label ‘Standard Customer,Financial Summary,Warehouse and Shipping,Finance Audit’;
SelectLayoutInstrLbl: Label ‘Select the layout to print’;

The chosen layout name is held by a SingleInstance codeunit that subscribes to ReportManagement.OnAfterSubstituteReport – the event the platform raises while resolving which report to run for a print. That subscriber is where the design-time selection is actually applied, and then immediately cleared:
codeunit 60106 “INK Layout Selection”
{
SingleInstance = true;
[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, ‘OnAfterSubstituteReport’, ”, false, false)]
local procedure ApplySelectedLayoutOnSubstituteReport(ReportId: Integer; var NewReportId: Integer)
var
DesigntimeReportSelection: Codeunit “Design-time Report Selection”;
begin
if not HasSelection then
exit;
if ReportId <> Report::”Standard Sales – Invoice” then
exit;
DesigntimeReportSelection.SetSelectedLayout(SelectedLayoutCaption);
ClearSelection();
end;
procedure SetLayout(LayoutCaption: Text)
begin
SelectedLayoutCaption := LayoutCaption;
HasSelection := true;
end;
procedure ClearSelection()
begin
Clear(SelectedLayoutCaption);
HasSelection := false;
end;
var
SelectedLayoutCaption: Text;
HasSelection: Boolean;
}

Three guards make this safe. SingleInstance keeps the chosen layout alive across the boundary from the page action to the platform’s report-resolution step. The HasSelection check means the subscriber does nothing unless a layout was deliberately chosen, so it never disturbs unrelated report runs. The ReportId check restricts the effect to the one report we mean to touch – OnAfterSubstituteReport fires broadly, so guarding on the report is essential. Finally, ClearSelection runs right after the layout is applied, so the choice affects this run only and cannot leak into the next report the user prints.
Step 4: Build, publish, and test
Build and publish the extension, then open any posted sales invoice. Test both paths. For Approach A, press each of the promoted print buttons and confirm the matching layout renders. For Approach B, use the page’s Print action, choose an entry from the menu, and confirm the same. Then verify a couple of negative cases: run the standard report from somewhere else (for instance, from a report request page directly) and confirm your interceptor leaves it alone, and print two different layouts back to back to confirm the previous selection never carries over.
Considerations and Constraints
The technique is small, but a few characteristics are worth designing around:
- The selection is session state, so scope it tightly. SetSelectedLayout affects the next render in the session. Set it immediately before the run and, in the interception pattern, clear it immediately after – as ClearSelection does – so a stale choice never lands on an unrelated report.
- The layout name must match exactly. A typo or a translated string silently falls back to the default layout rather than erroring. Keep the names in Locked labels and reuse the exact names from the rendering section.
- Always guard OnAfterSubstituteReport on ReportId. The event is raised for report resolution broadly; without the ReportId check (and the HasSelection check) a subscriber can interfere with reports it was never meant to touch.
- This is version-specific. It uses modern report layouts and the rendering section, which require Business Central 2022 release wave 1 or later. The older “Custom Report Layout” path (SetTempLayoutSelected) is a different, legacy mechanism; on current versions prefer Design-time Report Selection.
- Layout selection changes presentation, not data. Every field and total a layout displays must already exist in the (extended) report dataset. If a layout needs a new value, widen the dataset first – selecting the layout will not add it.
- Test across clients and output paths. Confirm the behaviour on the clients your users actually use, and for both preview and print/PDF, since the report request and Send-to flows can differ.
Conclusion
Choosing a report layout from AL turns out to be a one-line platform call – SetSelectedLayout on the “Design-time Report Selection” codeunit – wrapped in one of two patterns. When your code launches the report, set the layout and run it in the same breath. When you need the built-in Print action to honour a coded choice, capture the choice in a single-instance codeunit and apply it in an OnAfterSubstituteReport subscriber, guarded to the right report and cleared straight after. Either way you keep a single report with a single dataset, register your layouts once, and let the record – or the button the user pressed – decide how the document should look.