Substituting standard reports

by Jun 20, 2022AL Language, Reports

Home 9 Development 9 AL Language 9 Substituting standard reports

Report objects cannot be extended in versions previous to the Business Central 2021 release wave 1 version when ReportExtensions object was introduced. Since then, many of changes we need to do in reports can be done without creating a new copy of object.

On the other hand, there are definitely still many occasions where it’s appropriate (or even necessary) to create a new copy of the existing object. Afterwards, you may subscribe to the OnAfterSubstituteReport event, which is issued by Codeunit 44 – ReportManagement, to override the basic report.

Using another report as a replacement

Subscribing to the OnAfterSubstituteReport event will allow you to replace a report, as seen in the code below. Using the OnSubstituteReport publisher, you may replace a report’s ReportId with a new one. The original “Item Price List” will be replaced by “My Item Price List”.

[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterSubstituteReport', '', false, false)]
local procedure OnAfterSubstituteReportReportManagement(ReportId: Integer; var NewReportId: Integer)
begin
    if ReportId = Report::"Item Price List" then
        NewReportId := Report::"My Item Price List";
end;

Important info: In my opinion, the name of the publisher (OnAfterSubstituteReportEvent) is not correct. Microsoft says:

The event is called OnAfterSubstituteReport to match the pattern followed by other events in the ReportManagement codeunit, but the subscriber will be invoked before the substitution takes place.

Microsoft (Substituting Reports)

But the name just do not make any sense as it is definitely happening BEFORE report substitution or even better – OnSubstituteReport. Let’s hope Microsoft will change it in the feature…

The report to be replaced is launched by the user through a page action with the RunObject Property assigned to the report, through “Tell me” search window or if the report is run using any of standard methods (Run, Print, SaveAsXXX, ….).

Good things to know

It’s always a good idea to have report’s caption property with the same caption in both reports. Therefore, all links and captions pointing to the report will be in sync with the report’s actual content. Even when the original report is replaced with one with a different caption, bookmarks linking to the report retain the previous report’s caption.

Furthermore, it is good practice to verify whether the report has been already substituted with another extension: Prior to making a change, check the value of the Reported and NewReportId parameters. If the NewReportId variable is different from -1, it indicates that the report has been replaced in another event subscriber. This is important, because the order of subscribers that subscribe to the same publisher is unpredictable if you do not check the property, it could cause that if you print the report twice, you can get two different reports called.

[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterSubstituteReport', '', false, false)]
local procedure OnAfterSubstituteReportReportManagement(ReportId: Integer; var NewReportId: Integer)
var
    MyNewReportId: Integer;
begin
    if ReportId = Report::"Item Price List" then begin
        MyNewReportId := Report::"My Item Price List";
        if NewReportId <> -1 then begin
            // Inform user that there are two reports from two different extensions that are replacing the same source report.
            // Let them choose the one they want to use or return an error that only one is possible and that they should contact authors of both extensions.
            exit;
        end;
        NewReportId := MyNewReportId ;
    end;
end;

Recent Articles from the category

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here