Dynamics 365 Business Central: How to copy values (Blob) ​​from one Codeunit “Temp Blob” to another Codeunit “Temp Blob”

Dynamics 365 Business Central

Hi, Readers.
Today I want to share a simple topic, how to copy values ​​from one Codeunit “Temp Blob” to another Codeunit “Temp Blob”.

Starting from 2019 Release Wave 2 (BC15), Microsoft deprecated ‘Record TempBlob’ and introduced a new Codeunit “Temp Blob”. More details: BLOB Storage Module

Codeunit “Temp Blob”: The container to store BLOB data in-memory.  when you’re writing code to manage the importing and exporting of files, you can use this codeunit.

The supported methods in the Temp Blob codeunit include:

  • CreateInStream – Creates an InStream object to read data from the BLOB. You can optionally provide an Encoding parameter to this function.
  • CreateOutStream – Creates an OutStream object to write data to the BLOB. You can optionally provide an Encoding parameter to this function.

Last week when I was testing Dynamics 365 Business Central: How to get the page number of a PDF or Word file in AL (Display the page count on Attached Documents and Doc. Attachment List Factbox), I made a mistake at the beginning. I converted the variable TempBlob in Event directly into PDF (in order to get the number of pages), but this will cause an error when actually importing BC.

Microsoft Office document cannot be imported. File header is corrupt (MIME type: application/vnd.openxmlformats-officedocument.wordprocessingml.document.

So I need to pass Tempblob to another variable. This is not very difficult. Here is the solution:

Simply put, this only requires the following three steps.

    procedure CopyTempBlob(SourceTempBlob: Codeunit "Temp Blob"; var TargetTempBlob: Codeunit "Temp Blob")
    var
        InStr: InStream;
        OutStr: OutStream;
    begin
        // Create InStream From Source TempBlob
        SourceTempBlob.CreateInStream(InStr);
        // Create OutStream From Target TempBlob
        TargetTempBlob.CreateOutStream(OutStr);
        // Copy data from source stream to target stream
        CopyStream(OutStr, InStr);
    end;

Great, give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL