Mastering InStream and OutStream: Efficient Data Handling in Business Central

Mastering InStream and OutStream: Efficient Data Handling in Business Central

In Microsoft Dynamics 365 Business Central, handling binary data—such as images, documents, or other files—is commonly achieved using InStream and OutStream. These data types are pivotal for managing file input and output operations, especially when dealing with Blob fields.

  • InStream: This is used to read data from a Blob field or file. It allows you to process binary data by reading from a stream of data.
  • OutStream: This is used to write data to a Blob field or file. It enables you to store binary data by writing to a stream of data.

These stream types are essential when working with Blob fields in Business Central, making them crucial for tasks like saving files, generating reports, or processing images.

1. Reading Data with InStream

To read data from a Blob field, you use the InStream type. Here’s a simple example of how to read an image from a Blob field:

procedure ReadBlob()
    var
        MyTable: Record "My Table";
        InStream: InStream;
        FileContent: Text;
        FileCount: Integer;
    begin
        if MyTable.Get(1) then begin // Assuming record with ID 1 exists
            if MyTable."Image".HasValue() then begin
                MyTable."Image".CreateInStream(InStream);
                // Read the InStream and process it
                FileCount := InStream.ReadText(FileContent, 1024); // Example method, use appropriate handling
                Message('Blob content read successfully: %1', FileContent);
            end;
        end;
    end;        

2. Writing Data with OutStream

To write data to a Blob field, you use the OutStream type. Here’s an example of how to write a text string to a Blob field:

 procedure WriteBlob()
    var
        MyTable: Record "My Table";
        OutStream: OutStream;
        FileContent: Text;
    begin
        FileContent := 'This is an example content'; // Data to be written to Blob
        if MyTable.Get(1) then begin // Assuming record with ID 1 exists
            MyTable."Image".CreateOutStream(OutStream);
            OutStream.WriteText(FileContent); // Write text to the Blob
            MyTable.Modify();
            Message('Blob content written successfully.');
        end;
    end;        

3. Converting Data to Blob and Sending Reports

For generating reports and sending them as email attachments, you typically save the report to a Temp Blob and then send it. Here’s a concise example:

a. Convert XML file into Blob field

 procedure ConvertStringToBlob()
    var
        MyTable: Record "My Table";
        OutStream: OutStream;
        XmlContent: Text;
    begin
        XmlContent := '<root><element>Example</element></root>';
        if MyTable.Get(1) then begin // Assuming record with ID 1 exists
            MyTable."Image".CreateOutStream(OutStream);
            OutStream.WriteText(XmlContent); // Convert XML content to Blob
            MyTable.Modify();
            Message('XML content saved to Blob successfully.');
        end;
    end;        

b. Save a report and send via Email attachment

procedure SaveReportToTempBlobAndEmail()
    var
        CustomerReport: Report "Customer - List";
        TempBlob: Codeunit "Temp Blob";
        OutStream: OutStream;
        InStream: InStream;
        Email: Codeunit Email;
        EmailAttachment: Record "Email Attachments";
        EmailMessage: Codeunit "Email Message";
        ReportParameters: Text;
    begin
        // Save the report to Temp Blob
        ReportParameters := CustomerReport.RunRequestPage();
        TempBlob.CreateOutStream(OutStream);
        Report.SaveAs(REport::"Customer - List", ReportParameters, ReportFormat::Pdf, OutStream);
        TempBlob.CreateInStream(InStream);


        // Attach report to email
        TempBlob.CreateInStream(InStream);
        EmailMessage.Create('[email protected]', 'Report Attached', 'Please find the attached report.');
        EmailMessage.AddAttachment('CustomerPDF.pdf', 'PDF', InStream);
        Email.Send(EmailMessage, Enum::"Email Scenario"::Default);
        Message('Report sent successfully.');
    end;
        

Summary

  • InStream helps in reading binary data from Blob fields.
  • OutStream assists in writing binary data to Blob fields.
  • Combining these with report handling allows seamless data manipulation and email integration.

By mastering InStream and OutStream, you can effectively manage binary data in Business Central, enhancing your ability to work with files and reports in the cloud environment.

Jon Long

Retired Business Central Solution Developer/Automated Testing Warrior

7 个月

Nice explanation!

要查看或添加评论,请登录

Senthil Kumar Varatharajan的更多文章

社区洞察

其他会员也浏览了