AI was used to create this answer
To handle document attachments between a customized portal and Microsoft Dynamics 365 Business Central (BC), you can use the following approaches:
To automatically attach documents sent from the portal to BC, you can use the Document Attachment
table and its related codeunits.
Table: Document Attachment
(Table ID: 1173)
Codeunit: Document Attachment Management
(Codeunit ID: 1175)
Prepare the Attachment:
Convert the document (e.g., PDF, Word, etc.) into a base64-encoded string or byte array.
Ensure you have the necessary metadata (e.g., document name, file extension, related record ID).
Call the Codeunit:
Use the Document Attachment Management
codeunit to create and attach the document to the relevant record in BC.
Example AL code snippet:
procedure AttachDocumentToRecord(RecordId: RecordId; FileName: Text; FileExtension: Text; FileContent: Text) var DocumentAttachment: Record "Document Attachment"; DocumentAttachmentManagement: Codeunit "Document Attachment Management"; InStream: InStream; TempBlob: Codeunit "Temp Blob"; begin // Convert base64 content to a stream TempBlob.FromBase64String(FileContent); TempBlob.CreateInStream(InStream); // Create and attach the document DocumentAttachment.Init(); DocumentAttachment."Table ID" := RecordId.TableNo; DocumentAttachment."No." := RecordId.RecordKey; DocumentAttachment."File Name" := FileName; DocumentAttachment."File Extension" := FileExtension; DocumentAttachment."Document Reference ID".ImportStream(InStream, FileName); DocumentAttachment.Insert(true); // Optionally, validate the attachment DocumentAttachmentManagement.ValidateAttachment(DocumentAttachment); end;
Expose the Codeunit as a Web Service:
Publish the codeunit as a web service in BC so that the portal can call it.
To retrieve attachments from BC and display them on the portal, you can use the same Document Attachment
table and codeunit.
Fetch Attachments:
Query the Document Attachment
table to retrieve attachments linked to a specific record.
Example AL code snippet:
procedure GetDocumentAttachments(RecordId: RecordId; var DocumentAttachments: List of [Text]) var DocumentAttachment: Record "Document Attachment"; TempBlob: Codeunit "Temp Blob"; OutStream: OutStream; Base64Content: Text; begin DocumentAttachment.SetRange("Table ID", RecordId.TableNo); DocumentAttachment.SetRange("No.", RecordId.RecordKey); if DocumentAttachment.FindSet() then repeat TempBlob.CreateOutStream(OutStream); DocumentAttachment."Document Reference ID".ExportStream(OutStream); Base64Content := TempBlob.ToBase64String(); DocumentAttachments.Add(Base64Content); until DocumentAttachment.Next() = 0; end;
Expose the Codeunit as a Web Service:
Publish the codeunit as a web service to allow the portal to call it and retrieve the attachments.
Portal Integration:
Call the BC web service from the portal to fetch the attachments.
Decode the base64 content and display or download the document on the portal.
Permissions: Ensure the portal has the necessary permissions to access BC web services.
File Size: Handle large files efficiently by chunking or compressing them if needed.
Error Handling: Implement robust error handling for failed uploads or downloads.
Security: Use secure communication (HTTPS) and validate user inputs to prevent injection attacks.
By using the Document Attachment
table and Document Attachment Management
codeunit, you can seamlessly integrate document attachments between your portal and BC.
André Arnaud de Cal...
292,494
Super User 2025 Season 1
Martin Dráb
231,313
Most Valuable Professional
nmaenpaa
101,156