US 408 365 4638

Get Attachment Document from Business Central using A/L

Table of Contents

In Business Central there are two ways to attach documents (pdf, txt and more) to respective sales order, purchase order or Posted Sales Invoice and more.

  1. Incoming Document
  2. Attachments

Introduction to Incoming Document:

Snapshot for Incoming Document for Posted Sales Invoice Page:

We can view already attached incoming document using View Incoming Document Option as highlighted in above snapshot. The Documents which are going to be attached using Incoming Document Option would be accessed using Incoming Document (130) and Incoming Document Attachment Table. We will discuss how we can access documents using AL later.

Introduction to Attachments:

Snapshot for Attachments for Posted Sales Invoice Page:

We can view already attached attachments using Attachments Option as highlighted in above snapshot. The Documents which are going to be attached using Attachments Option would be accessed using Document Attachment (1173) Table. 

Get Incoming Document for Posted Sales Invoice using AL:

In this tutorial we will be storing document content in the form of base64 string as a final output.

1) First step will be to define required variables, we will be using following variables for fetching Document.


-> Incoming Document Attachment (Type : Record) for fetching document for specific posted invoice.
-> Temp Blob (Type : Codeunit) for creating and managing OutStream and InStream Objects.
-> InStream object
-> Base64 Convert for converting InStream to base64 String.

2) Second step is to set range on Incoming Document Attachment table for fetching specific invoice.

3) Third step to apply FindFirst function on Incoming Document Attachment table.

4) Actual content of document is placed in “Content” field of Incoming Document Attachment table which is a FlowField so for that we will have to use CalcField function after filtering the record using SetRange and FindFirst and then store it in Temp Blob.

5)Then create InStream using tempBlob

6) Finally we need to convert that InSream to Base64 String for that we will use Base64 Convert.

Full Code Snippet for fetching Document from Incoming Document Attachment Table and converting it into Base64 String. 

Get Attachment for Posted Sales Invoice using AL:

In this tutorial we will be storing document content in the form of base64 string as a final output.

1) First step will be define required variables, we will be using following variables for fetching Document.


-> Document Attachment (Type : Record) for fetching document for specific posted invoice.
-> Temp Blob (Type : Codeunit) for creating and managing OutStream and InStream Objects.
-> OutStream object
-> InStream object
-> Base64 Convert for converting InStream to base64 String.

2) Second step is to set range on Document Attachment table for fetching specific invoice.

3) Third step to apply FindFirst function on Document Attachment table.

4) Actual content of document can be accessed using “Document Reference ID” which is a media type field in Document Attachment table. Initially we will create empty OutStream using tempBlob ad then write content of actual document on OutStream using export function as follow.

5) Then create InStream using same tempBlob object.

6) Finally convert that InStream to base64 String using Base64 Convert.

Full Code Snippet for fetching Document from Document Attachment Table and converting it into Base64 String.

Related Post