Business Central - 'OnCustomDocumentMergerEx' even does not trigger

82 Views Asked by At

I Recently tried to refactor a deprecated part of our code, which is an event subscription to 'OnBeforeMergeDocument', Because i had some problems regarding the printer name which I posted about in this Stack Overflow post.

I then tried to bind to the new event using the following code

    [EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnCustomDocumentMergerEx', '', true, true)]
    local procedure OnCustomDocumentMergerEx(ObjectID: Integer; ReportAction: Option SaveAsPdf,SaveAsWord,SaveAsExcel,Preview,Print,SaveAsHtml; ObjectPayload: JsonObject; XmlData: InStream; LayoutData: InStream; var DocumentStream: OutStream; var IsHandled: Boolean)
    var
        Test: Text;
    begin
        Test := 'test';
        IsHandled := true;
    end;

Just like the 'OnBeforeMergeDocument' event, i expected it to fire when i preview or print or send a report (for example if you go to business central > posted sales invoices > print/send > print). However it doesnt, and im getting a 'The custom report layout for '' is empty.' why is this? and why doesnt the event fire when I think it would fire?

1

There are 1 best solutions below

0
On

This behaviour is pretty much different for on-prem/SaaS deployment and versions from 19 to 21. I am assuming that you are running a SaaS instance which is updated to the latest release (21.3). In this version, preparation of the report layout is handled by the platform by default, and application events are not triggered. To change the flow, subscribe to the event ApplicationReportMergeStrategy in codeunit Reporting Triggers and change the merge strategy to Application.

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Reporting Triggers", 'ApplicationReportMergeStrategy', '', false, false)]
    local procedure SetMergeStrategy(ObjectId: Integer; LayoutCode: Text; var InApplication: Boolean)
    begin
        // if ObjectId = ... - if the strategy is set for a specific report object
        InApplication := true;
    end;

Once the merge strategy is changed to Application, the event OnCustomDocumentMergerEx starts firing.