How to attach a blob file from Azure to Business Central (Document Attachment)?

Introduction:

This blog has three parts

  1. Copy a file from one azure container to another container placed in the same storage location
  2. Create a Purchase Invoice and attach the azure blob in the document attachment
  3. Delete the blob file from the source container.

Pre-requisites:

  1. Visual Studio Code (VS Code)
  2. Business Central SaaS environment
  3. Files uploaded to one container

Steps:

Click here to refer Part 1

Part 2 : Create a Purchase Invoice and attach the azure blob file in the document attachment

Please complete the the basic setups in Azure and Business central mentioned in Part 1.

  1. Create a setup table in Business Central to enter the storage account, source and destination containers with the access key like below.

2. Create a setup page with action button to trigger the code to create Invoice

3. Create a page extension for Document attachment to download the blob file.

4. In the below code snapshot, we are storing the shared access key in an Interface Storage service Authorization and initializing the Azure Blob client to read the source and destination container

5. Get a List of blobs from ABS Container Container Content and create a loop to get the blob from the source container as stream and use PutBlobBlockBlobStream function of the destination blob client to copy the stream file to the destination.

6. Create a function to create a invoice and to get the blob file as stream and attach to the purchase invoice as a document attachment.

7. Create a Purchase Header from line 48 to 51. Get the blob as stream on line71 and import the stream to document attachments and finally delete the blob file from the source in line 77

8. Publish the code.

9. Run the Job action button on the setup page

Leave a comment