Media object cannot be imported because the instream object is empty.

I'm trying to save the report as pdf in the table with a media type. I put the code in the event and when posting the bank recon, it give me the error "media object cannot be imported because the instream object is empty"

The debugger stops at "AttachedDocumentInsert."Document Reference ID".ImportStream(BlobInStream, '');"

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Bank Acc. Reconciliation Post", 'OnAfterFinalizePost', '', false, false)]
    local procedure Cod370_OnAfterFinalizePost(var BankAccReconciliation: Record "Bank Acc. Reconciliation")
    var
        TempBlob_lRec: Codeunit "Temp Blob";
        BlobOutStream: OutStream;
        BlobInStream: InStream;
        RecRef: RecordRef;
        FileMgt: Codeunit "File Management";
        BankRecon: Record "Bank Acc. Reconciliation";
        AttachedDocumentInsert: Record "Bank Recon. Doc. Attached";
        LineNo: Integer;
    begin

        BankRecon := BankAccReconciliation;
        BankRecon.SetRecFilter();
        RecRef.GetTable(BankRecon);

        TempBlob_lRec.CreateOutStream(BlobOutStream);
        Report.SaveAs(Report::"Bank Acc. Recon. - Test", '', ReportFormat::Pdf, BlobOutStream, RecRef);

        TempBlob_lRec.CreateInStream(BlobInStream);
        
        AttachedDocumentInsert.Init();
        AttachedDocumentInsert."Table ID" := Database::"Bank Acc. Reconciliation";
        AttachedDocumentInsert."Line No." := 1;
        AttachedDocumentInsert."No." := BankRecon."Bank Account No.";
        AttachedDocumentInsert."Statement No." := BankRecon."Statement No.";
        AttachedDocumentInsert."Attached Date" := CurrentDateTime;
        AttachedDocumentInsert."File Name" := 'BankRecon';
        AttachedDocumentInsert.Validate("File Extension", 'pdf');
        AttachedDocumentInsert."Document Reference ID".ImportStream(BlobInStream, '');
        AttachedDocumentInsert.Insert();
    end;

But when I tried to put the code in action, it doesn't give me the error.

Any idea why it gives an error in events?

Thank you in advance.

Related
Recommended