OnAfterIntermediateDocumentReady Event

This article describes the syntax of the OnAfterIntermediateDocumentReady event and the attributes of the report payload.

Usage

Use the OnAfterIntermediateDocumentReady event to specify what happens when the user has generated an intermediate report artifact by stream or file, from code or a request page action. This will typically be Xml or Word files where they are genereated in the specific process flows. The OnAfterIntermediateDocumentReady event is used to enable document patching scenarios in the application, or to copy the artifact to a different location during testing.

The event input is the report ID, a JSON collection with report runtime information and the generated document in an InStream. Use the documenttype JSON property to identify the data type stored in the DocumentStream parameter and act accordingly. The final result must be written to the TargetStream parameter and the parameter Success must be set to true if the modified stream is to be used in the platform. The content in the TargetStream will be discarded if the Success parameter is false upon return from the procedure.

Note

Changing the content type isn't allowed.

Publisher

Codeunit 44 ReportManagement.

Raised

When the report runtime has generated an intermediate output artifact that'll be removed when the final artifact have be created. This can occur when the application runs a SaveAs method, the user invokes one of the SendTo actions in the report request page, or when the document is being printed using an extension printer or by using the web client print capability.

Syntax

[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterIntermediateDocumentReady', '', true, true)]
local procedure OnAfterIntermediateDocumentReady(ObjectId: Integer; ObjectPayload: JsonObject; DocumentStream: InStream; var TargetStream: OutStream; var Success: Boolean)

Parameters

ObjectID

Type: Integer

The ID of the report object to be run.

ObjectPayload

Type: JsonObject

Instance of the report payload. For more information, see Report payload structure.

DocumentStream

Type: InStream

A stream object that contains the generated artifact. The actual data type can be found from the MIME type in the report payload document.

TargetStream

Type: OutStream

Target stream that must contain the modified artifact if the application code handles the object and returns success = true.

Success

Type: Boolean

Specifies whether the extension handled the generated artifact successfully.

Report payload

The report payload contains metadata for the report object and a list of attributes that identifies that current invocation. The payload consists of several attributes and has the following structure.

{
    "filterviews":
    [
        {"name":"Header","tableid":112,"view":"VERSION(1) SORTING(Field3) WHERE(Field3=1(103027))"},
        {"name":"Line","tableid":113,"view":"VERSION(1) SORTING(Field3,Field4) WHERE(Field4=1(0..10000))"},
        {"name":"ShipmentLine","tableid":7190,"view":"VERSION(1) SORTING(Field1,Field2,Field3) WHERE(Field2=1(10000))"}
    ],
    "version":1,
    "objectname":"Standard Sales - Invoice",
    "objectid":1306,
    "documenttype":"application/pdf",
    "invokedby":"00000000-0000-0000-0000-000000000001",
    "invokeddatetime":"2020-01-17T15:33:52.48+01:00",
    "companyname":"CRONUS International Ltd.",
    "printername":"My Printer",
    "duplex":false,
    "color":false,
    "defaultcopies":1,
    "papertray":
    {
        "papersourcekind":257,
        "paperkind":0,
        "landscape":false,
        "units":0,
        "height":1268,
        "width":929
    },
    "intent":"Print",
    "layoutmodel": "Rdlc",
    "layoutname": "RDLCLayout",
    "layoutmimetype": "Application/ReportLayout/Rdlc",
    "layoutapplicationid": "00001111-aaaa-2222-bbbb-3333cccc4444",
    "reportrunid": "6a3742c1-65ef-44a6-af58-3ef4a902932b"
}

Attributes

objectname

Specifies the name of the object.

objectid

Specifies the ID of the object.

documenttype

Specifies the MIME type of the document. Currently only application/pdf is supported for printing, other intents can have types specific to the report layout model and selected output target type.

invokedby

Specifies the ID of the user who invoked the print action.

invokeddatetime

Specifies the date and time that the print action was invoked, for example, 2019-10-22T22:25:54.338+02:00. The value is the date and time on the client machine.

companyname

Specifies the Business Central company.

version

Specifies the version of the report payload. Currently, the only supported version for the payload is 1.

duplex

Specifies whether to use duplex (2-sided) printing. true specifies duplex printing; otherwise false.

Note

duplex is currently not used and ignored at runtime.

color

Specifies whether color printing is set. true specifies color printing; otherwise false.

Note

color is currently not used and ignored at runtime.

defaultcopies

Specifies the number of copies to print by default. The default is 1.

Note

defaultcopies is currently not used and ignored at runtime.

filterviews

Specifies views that are used on the document.

name

Specifies the name of data item that the filter view applies to.

tableid

Specifies the ID of the table for the view.

view

Specifies the name of the view.

papertray

Specifies the paper tray settings from the printer payload.

papersourcekind

Specifies a standard paper source for the paper tray. The value can be either the text representation (such as AutomaticFeed) or the corresponding number (such as 7).

paperkind

Specifies the paper size. The value can be either the text representation (such as A4) or the corresponding number (such as 9).

landscape

Specifies whether landscape orientation is used. true specifies landscape orientation; otherwise false.

units

Specifies the units of measurement for values in height and width attributes. Valid values include:

  • HI for hundredths of an inch (default value).
  • TI for thousandths of an inch.
  • IN for inches.
  • CM for centimeters.
  • MM for millimeters.
  • PC for picas.
  • PT for points.

height

Specifies the height of the paper. The values include positive integers up to 2,147,483,647.

width

Specifies the width of the paper. The values include positive integers up to 2,147,483,647.

intent

Specifies the intent of the current report invocation, which can be one of the following values.

  • None
  • Print
  • Preview
  • Save
  • Schedule
  • Download
  • Parameters

layoutmodel

Specifies the layout model selected for teh current report invocation, which can be one of the following values.

  • Rdlc
  • Word
  • Excel
  • Custom

layoutname

Specifies the layout name for the currently selected layout.

layoutmimetype

Specifies the layout mimetype for the currently selected layout. The built-in layout types are:

  • Application/ReportLayout/Rdlc
  • Application/ReportLayout/Word
  • Application/ReportLayout/Excel

layoutapplicationid

Specifies the ID of the application that provides the selected layout.

reportrunid

Specifies a unique ID of type Guid that identifies the current report invocation. This value can be used to correlate calls to report event that support json payloads.

Sample code

Subscribe to the standard event in codeunit ReportManagement. This sample saves the JSON payload to a text file and copies the document stream content to the target stream.

[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnAfterIntermediateDocumentReady', '', true, true)]
local procedure OnAfterIntermediateDocumentReady(ObjectId: Integer; ObjectPayload: JsonObject; DocumentStream: InStream; var TargetStream: OutStream; var Success: Boolean)
var
    JsonText: Text;
    JsonFile: File;
    mimeType: Text;
    ReportIntentJson: JsonToken;
    ReportIntentTxt: Text;
    SharedDocumentStream: InStream;
begin
    // Honor the standard handle pattern
    if Success = true then
        exit;

    // Empty stream, no actions possible on the stream so return immediatly
    if DocumentStream.Length < 1 then
        exit;

    // Use a shared stream and reset the read pointer to beginning of stream.
    SharedDocumentStream := DocumentStream;
    if SharedDocumentStream.Position > 1 then
        SharedDocumentStream.ResetPosition();
    
    // Save the report payload in a text file.
    ObjectPayload.WriteTo(JsonText);
    JsonFile.TextMode := true;
    JsonFile.Create('c:\temp\OnAfterDocumentReady.json', TextEncoding::UTF8);
    JsonFile.Write(JsonText);
    JsonFile.Close();

    ObjectPayload.Get('intent', ReportIntentJson);
    ReportIntentTxt := ReportIntentJson.AsValue().AsText();

    case ReportIntentTxt of
        'Save':
            HandleSaveScenario();
        'Download':
            HandleDownloadScenario();
        'Print':
            HandlePrintScenario();
    end;
    
    // do something on the document stream and write it back to the serverr.
    CopyStream(TargetStream, SharedDocumentStream);
    Success := true;
end;

See Also

Events in AL
Publishing Events
Raising Events
Subscribing to Events