Dynamics 365 Business Central: How to substitute a report for another report via AL

Dynamics 365 Business Central

Hi, Readers.
Last week we discussed how to select a report layout before printing in Business Central.
Today I would like to talk about another interesting topic about Report, how to substitute a report for another report via AL.

As you might know, in versions prior to Business Central 2021 release wave 1 (BC18) extensibility is not supported for report objects. Therefore, if you want to make any changes to the dataset you must create a new version of the report and apply the changes on the new object. More details: Report extensibility (ReportExtension Object)
But Report Extension is not omnipotent, for some core changes we may still need to copy the original report and recreate a new version.

For example, I copied Standard Sales – Order Conf. (1305) to create a new report (report 50111 “ZY Standard Sales Order Conf.”).

If we need to substitute the old report, we may create a new action and hide the original action from the page. Or just let the user search for the new report to use.

PS: In Report Selection – XXXX features, we can specify special reports and layout files according to Usage. But please note that this does not apply to all reports.

For example,

In this post, I want to introduce another method, substitute a report for another report via AL. It’s not very difficult.

In codeunit 44 ReportManagement, we can find an integration event, OnAfterSubstituteReport().

So to substitute a report, just create a method and subscribe it to the OnAfterSubstituteReport event, as shown in the code below.

Source Code: When the OnAfterSubstituteReport event is raised, the event subscriber method is called and the substitution takes place.

codeunit 50112 "ZY Substitute Report"
    [EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterSubstituteReport', '', false, false)]
    local procedure OnSubstituteReport(ReportId: Integer; var NewReportId: Integer)
        if ReportId = Report::"Standard Sales - Order Conf." then
            NewReportId := Report::"ZY Standard Sales Order Conf.";

Test Video: When the extension is installed, the original report will be substituted.

It’s very easy, and you can add some settings to your Extension, and when the settings are enabled, replace the original report. Give it a try!!!😁 Believe your solution will be better.

PS: You can find more about Substituting Reports in MS Docs:

The OnAfterSubstituteReport event is raised when:

  1. The user activates a page action that runs the report to be substituted, that is, an action that has the RunObject Property set to the report.
  2. The report is invoked from the Tell Me window.
  3. The report is called by one of the following static methods:


Hope this will help.

Thanks for reading.