Mastering InStream and OutStream: Efficient Data Handling in Business Central
Senthil Kumar Varatharajan
Manager – D365 BC Technical Business Central at Mercurius IT| Microsoft Dynamics & SAP Solutions for better business | A safe pair of hands
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.
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
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.
Retired Business Central Solution Developer/Automated Testing Warrior
7 个月Nice explanation!