MSBA-352 · Lesson 7 of 12 · 20 min
Reports and report layouts
▶ Video coming soon A video version of this lesson is on the way.
Bettina Szczepanik had one request of her own for the warranty app. "When a café buys a twelve-hundred-dollar grinder from us, they should get something that looks like we take it seriously," she said, when Callum showed her the registration list in UPDATE-TEST. She meant a warranty certificate: one page, with the Corbel & Finch logo, the café's name, the machine and serial number, the start and end dates, and how to make a claim. Callum added a second request: a monthly list of warranties ending in the next 60 days, which the sales team could use to offer service plans. Both are reports, and they taught Joss how Business Central separates data from presentation.
How a report is built
A report object has up to four parts. The dataset defines which tables are read and which values, called columns, are passed to the layout. The request page is the dialog users see before running the report, with options and filters. The rendering section lists one or more layouts that turn the dataset into a document. And triggers run code at points such as before the report starts or after each record is read.
This separation is what makes Business Central reports flexible. The developer decides what data is available. Someone else, often a power user like Callum, can change how it looks by editing a Word or Excel layout without touching AL, as long as the columns they need are in the dataset.
The certificate dataset
namespace Quarrystone.CorbelFinch.Warranty;
using Microsoft.Foundation.Company;
using Microsoft.Inventory.Item;
using Microsoft.Sales.Customer;
report 50100 "CNF Warranty Certificate"
{
Caption = 'Warranty Certificate';
UsageCategory = Documents;
ApplicationArea = All;
DefaultRenderingLayout = CertificateWord;
dataset
{
dataitem(Registration; "CNF Warranty Registration")
{
RequestFilterFields = "Serial No.", "Customer No.";
column(SerialNo; "Serial No.") { }
column(StartDate; Format("Start Date", 0, 4)) { }
column(EndDate; Format("End Date", 0, 4)) { }
column(ShipmentNo; "Shipment No.") { }
dataitem(Item; Item)
{
DataItemLink = "No." = field("Item No.");
column(ItemNo; "No.") { }
column(ItemDescription; Description) { }
}
dataitem(Customer; Customer)
{
DataItemLink = "No." = field("Customer No.");
column(CustomerName; Name) { }
column(CustomerAddress; Address) { }
column(CustomerCity; City) { }
}
trigger OnPreDataItem()
begin
SetRange(Status, Status::Active);
end;
}
dataitem(CompanyInfo; "Company Information")
{
column(CompanyName; Name) { }
column(CompanyPhone; "Phone No.") { }
column(CompanyLogo; Picture) { }
column(ClaimText; ClaimInstructionsTxt) { }
trigger OnAfterGetRecord()
begin
CalcFields(Picture);
end;
}
}
rendering
{
layout(CertificateWord)
{
Type = Word;
LayoutFile = 'src/Layouts/WarrantyCertificate.docx';
Caption = 'Warranty certificate (Word)';
}
}
var
ClaimInstructionsTxt: Label 'To make a claim, call us or email us with the serial number shown above. Keep this certificate with the machine.';
}Data items are the tables the report loops over. The outer data item, Registration, produces one certificate per active registration that matches the user's filters. The nested data items, Item and Customer, are linked to the current registration by DataItemLink, so each pass reads exactly one item and one customer. A separate top-level data item reads Company Information once for the logo and phone number.
Columns are what the layout can see. A column can be a field, an expression or a variable. Joss formatted the dates in the dataset with Format and format number 4, which writes the date out with the month name in the user's language and regional format, so the layout does not need date logic. The claim instructions are a label, which means they can be translated with the rest of the app.
RequestFilterFields puts Serial No. and Customer No. on the request page as filters, so a user can print one certificate or all of a café's certificates. OnPreDataItem runs once before a data item starts reading, and Joss used it to restrict the report to active registrations whatever the user filtered. OnAfterGetRecord runs for each record; here it calculates the Picture field, which is a media field that has to be loaded explicitly.
Layout types
The rendering section declares the layouts the report ships with. Each has a name, a type, a file and a caption. DefaultRenderingLayout says which one runs unless a user or administrator chooses another.
| Layout type | Edited in | Best for | Notes |
|---|---|---|---|
| Word | Microsoft Word | Documents customers receive: certificates, letters, invoices | Fields are content controls mapped to dataset columns; tables repeat for nested data |
| Excel | Microsoft Excel | Lists and analysis people will sort, filter and pivot | The dataset lands on a data sheet; other sheets add pivots, charts and formulas |
| RDLC | Report Builder or Visual Studio | Pixel-precise, complex documents | Most powerful, hardest to edit; many standard documents still use it |
| Custom | Depends on the provider | Special outputs | Rare in per-tenant extensions |
For the certificate, Word was the obvious choice. Callum and Bettina could open the .docx, move the logo, change the wording around the fields and adjust fonts, and the result would still be filled from the dataset. Joss created the first version by building the app once with an empty layout file named in the rendering section; the compiler generates a Word document with the dataset structure embedded, which Joss then designed in Word by inserting content controls from the XML Mapping pane.
The expiry list in Excel
The sales team's monthly list needed no design at all, just data in a workbook they could filter. Joss wrote a second report with an Excel layout and one data item.
report 50101 "CNF Warranties Ending"
{
Caption = 'Warranties Ending Soon';
UsageCategory = ReportsAndAnalysis;
ApplicationArea = All;
DefaultRenderingLayout = EndingExcel;
DataAccessIntent = ReadOnly;
dataset
{
dataitem(Registration; "CNF Warranty Registration")
{
DataItemTableView = sorting("Customer No.", "End Date") where(Status = const(Active));
column(CustomerNo; "Customer No.") { IncludeCaption = true; }
column(CustomerName; "Customer Name") { IncludeCaption = true; }
column(ItemNo; "Item No.") { IncludeCaption = true; }
column(SerialNo; "Serial No.") { IncludeCaption = true; }
column(EndDate; "End Date") { IncludeCaption = true; }
column(DaysLeft; "End Date" - Today()) { }
trigger OnPreDataItem()
begin
SetRange("End Date", Today(), CalcDate(StrSubstNo('<+%1D>', DaysAhead), Today()));
end;
}
}
requestpage
{
layout
{
area(Content)
{
group(Options)
{
Caption = 'Options';
field(DaysAheadField; DaysAhead)
{
ApplicationArea = All;
Caption = 'Days ahead';
ToolTip = 'Specifies how many days ahead to look for warranties that end.';
}
}
}
}
trigger OnOpenPage()
begin
if DaysAhead = 0 then
DaysAhead := 60;
end;
}
rendering
{
layout(EndingExcel)
{
Type = Excel;
LayoutFile = 'src/Layouts/WarrantiesEnding.xlsx';
}
}
var
DaysAhead: Integer;
}Three new ideas appear. DataItemTableView fixes the sort order and a filter that users cannot remove. The requestpage section adds an option, Days ahead, defaulting to 60. And DataAccessIntent = ReadOnly tells Business Central the report only reads data, so in the online service it can run against a read-only replica of the database, keeping the load off the primary database that posting uses. Noor sets it on every report that does not write.
The Excel layout started as the generated workbook, with the dataset on a sheet called Data. Callum added a second sheet with a pivot table counting warranties by customer and month, and the sales team now opens a ready-made summary every month. When the dataset gains a column, the Data sheet gains it too, and Callum's pivot can use it.
Layouts users manage
Layouts are not frozen in the app. The Report Layouts page lists every layout for every report, including those shipped by extensions. Users with the right permissions can export a layout, edit it, and import it as a new user-defined layout, then set it as the default for the company in Report Layout Selection. Callum used this the week before the November 23 review to change the certificate's wording without waiting for a new build. The app's own layout stayed as the fallback.
This changes how Joss thought about layouts. The AL file holds the dataset, which is a contract with layout designers: columns can be added safely, but renaming or removing one breaks every user-defined layout that uses it. Noor's rule was that dataset columns, once released, are treated like public procedures.
Report extensions
Sometimes the right place for warranty data is a standard report. Callum wanted the serial numbers and warranty end dates printed on the sales shipment document that travels with the grinder. Microsoft owns that report, so Joss wrote a report extension.
reportextension 50100 "CNF Shipment Warranty" extends "Standard Sales - Shipment"
{
dataset
{
add(Line)
{
column(CNFWarrantyEnd; WarrantyEndTxt) { }
}
modify(Line)
{
trigger OnAfterAfterGetRecord()
begin
WarrantyEndTxt := WarrantyMgt.EndDatesForShipmentLine(Line);
end;
}
}
rendering
{
layout(CNFShipmentWithWarranty)
{
Type = Word;
LayoutFile = 'src/Layouts/ShipmentWithWarranty.docx';
Caption = 'Sales shipment with warranty dates';
}
}
var
WarrantyMgt: Codeunit "CNF Warranty Mgt.";
WarrantyEndTxt: Text;
}A report extension can add columns and data items to the dataset, add fields to the request page, add triggers, and ship new layouts. It cannot remove anything from the base report. The value comes from EndDatesForShipmentLine, a small procedure Joss added to the warranty management codeunit that lists the end dates of the serial numbers on a shipment line. The extra column is useless until a layout shows it, so the extension ships a copy of the standard shipment layout with one extra line per tracked item. Callum then selected the new layout for the shipment report in Report Layout Selection.
Joss checked the name of the shipment report in the symbols rather than from memory, because Business Central has kept several generations of document reports side by side. Noor added that the Report Selection - Sales page decides which report runs for each document usage, such as shipments. If a partner needs a completely different report rather than an extended one, the standard report management code also publishes an event that lets an app substitute one report ID for another.
Running and sending reports from code
Reports can be run from actions with RunObject, or from code with Report.Run or Report.RunModal. Code can also save a report directly to a file or a stream with Report.SaveAs, which is how Joss planned to attach certificates to emails. For version 1, Callum preferred to print from the registration list and email the PDF manually; automatic emailing, which uses Business Central's Email module and the customer's document sending profile, went onto the list for after go-live.
Worked problem
Choosing a layout type
Three requests arrive: a certificate for customers, a list of claims by month for Ysmay Thorvaldsen to analyse, and a detailed service report that must match a manufacturer's form to the millimetre. Which layout type fits each?
Show solution
- Certificate: Word. It is a document customers read, and Callum and Bettina can edit the design.
- Claims by month: Excel. Ysmay will filter, pivot and chart the data herself.
- Manufacturer's form: RDLC, because it needs precise positioning that Word layouts handle poorly.
- All three share the principle: the dataset carries every value, and the layout only arranges them.
Answer: Word for customer documents, Excel for analysis, RDLC for exact forms.
Request pages and saved settings
The request page is the report's front door, and it deserves the same care as any other page. Fields on the request page need captions and tooltips, sensible defaults set in OnOpenPage, and validation where a value could be wrong: Joss added a check that Days ahead is between 1 and 365. Filters on data items, set with RequestFilterFields, appear on a separate tab and let users narrow the report without the developer anticipating every case. Business Central remembers the last values a user entered, and users can save named settings for reports they run often, so the sales team saved a setting called Next 60 days for the ending-soon list. A report can also be scheduled from its request page to run later or on a recurrence, with the output stored in Report Inbox, which is how the monthly list now arrives without anyone remembering to run it.
Processing-only reports
Not every report prints. A report with ProcessingOnly = true has a dataset and a request page but no layout. It loops through data and runs code in its triggers, which makes it a convenient way to give users a batch job with filters and options. Many standard batch jobs, such as Adjust Cost - Item Entries, are processing-only reports. Joss considered one for the nightly expiry but chose the codeunit and job queue entry from lesson 5, because there were no options for a user to set. Noor's rule of thumb: if a batch job needs a request page, write a processing-only report; if it only needs a schedule, write a codeunit.
Reports and performance
Reports read a lot of data, and the dataset design decides how fast they run. Nested data items run their inner query once for each outer record, so a certificate run for 500 registrations reads Item and Customer 500 times each. That is fine for a few hundred rows and slow for a hundred thousand. For large summaries, a query (lesson 8) used as the data source is faster. Columns that are FlowFields are calculated for every row, so Joss kept them to the ones the layout really uses. And every reading report gets DataAccessIntent = ReadOnly.
Testing a layout
A layout is tested by running the report against data that stretches it. Joss created test registrations in the container with the longest customer name in Corbel & Finch's customer list, an address with three lines, and an item description that wraps, then printed certificates to PDF and checked that nothing was cut off or pushed onto a second page. Callum did the same in UPDATE-TEST with real cafés. The test found one problem: a café with a long legal name pushed the claim instructions onto a second page, which Callum fixed in his user-defined layout by reducing the logo size.
Common mistakes
MistakeCalculating values inside the layout.
InsteadAdd a column to the dataset and keep layouts free of logic.
MistakeRenaming a dataset column after users have built layouts on it.
InsteadTreat released columns like public procedures; add new ones instead.
MistakeLeaving DataAccessIntent at its default on read-only reports.
InsteadSet ReadOnly so the report can use a read-only replica.
MistakeCopying a standard report to add one column.
InsteadUse a report extension and ship a layout that shows the new column.
MistakeForgetting CalcFields on media and FlowFields used as columns.
InsteadCalculate them in OnAfterGetRecord.
Summary and key terms
- A report has a dataset, an optional request page, one or more layouts and triggers.
- Data items loop over tables; nested data items are linked with DataItemLink; columns feed the layout.
- Word layouts suit customer documents, Excel layouts suit analysis, and RDLC suits precise forms.
- Users can add and select layouts without code, so dataset columns are a contract.
- Report extensions add columns, data items, request page fields and layouts to standard reports.
- DataAccessIntent = ReadOnly lets reading reports run on a read-only replica.
- Dataset
- The data items and columns a report makes available to layouts.
- Data item
- A table a report reads, optionally linked to a parent data item.
- Request page
- The options and filters dialog shown before a report runs.
- Rendering layout
- A Word, Excel, RDLC or custom file that formats the dataset.
- Report extension
- An object that adds to an existing report's dataset, request page and layouts.
- Report Layout Selection
- The page where the layout used by each report is chosen.
- DataAccessIntent
- A property that lets read-only reports and queries use a read-only replica.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Why are dates formatted in the certificate dataset instead of in the Word layout?
Show answer
Logic belongs in the dataset; the layout only arranges values, and formatting in AL respects the user's language.
How did Callum change the certificate wording without a new build?
Show answer
He exported the layout from Report Layouts, edited it in Word, imported it as a user-defined layout and selected it as the default.
What does the shipment report extension add, and why does it also ship a layout?
Show answer
A warranty end date column on shipment lines; the column only appears if a layout shows it, so it ships an updated layout.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.