Custom API add attachement

Hello,

I am trying to add an attachement to a purchase order through a web service in business central.

I have tried the “attachements” api and this api can only add documents in the “Incoming documents” which only support certain document types (journal, purchase invoice).

So now I am trying to add custom APIs to add this attachement to the purchase order.

Does anyone have a documentation on how to do this?

I suppose an entry needs to be created in the “Tenant media” table and then in the “Document attachement” table but I am not sure of this.

Thanks for your help.

Omar

To add an attachment to a purchase order through a web service in Business Central, you will need to create a custom API to handle this process.

Firstly, you are correct that you need to create an entry in the “Tenant Media” table to store the attachment file. This table stores all media files associated with a particular tenant.

After storing the attachment file, you will then need to create an entry in the “Document Attachment” table to link the attachment to the relevant purchase order. The “Document Attachment” table contains information about attachments linked to documents in Business Central.

To create the custom API, you can follow these steps:

  1. Create a new codeunit in Business Central.
  2. Define a function in the codeunit to handle the attachment process. This function should take in the necessary parameters, such as the purchase order number and the attachment file.
  3. In the function, you can use the “Tenant Media” table to store the attachment file. You can do this by creating a new record in the table and setting the “File Name” and “Data” fields to the appropriate values.
  4. Next, you will need to create a record in the “Document Attachment” table to link the attachment to the purchase order. You can do this by creating a new record in the table and setting the appropriate fields such as “Document Type”, “Document No.”, “Attachment No.” (this should be the primary key of the record in the “Tenant Media” table), and any other necessary fields.
  5. Finally, you can expose this function as a web service by creating a new web service page and publishing it. You can then call this web service to add attachments to purchase orders.

Keep in mind that you will need to ensure that the user calling the web service has the necessary permissions to create records in the “Tenant Media” and “Document Attachment” tables. Additionally, you may want to implement error handling in your code to handle any potential issues that may arise during the attachment process.