Home Blog Learn How to Attach\Update\Remove\Delete Attachment Files in Acumatica

Learn How to Attach\Update\Remove\Delete Attachment Files in Acumatica

Yuriy Zaletskyy | April 13, 2023

YuriiBlogImage

Moving in customization world of Acumatica it is usual question how to read or write attachments in Acumatica. So, I want to share this example how you can read, delete and save files as attachment of Acumatica’s screen (I will use Shipments screen for example).

Acumatica allows user to attach additional objects to a master data record — for instance, a text note or a file.

Some tables already have this possibility. While you may need to turn on support attachments for each particular table individually.

To turn on support for data record attachments, add a NoteID field to DAC and NoteID column with the unique identifier data type to database table. Acumatica stores attachments in separate tables, therefore the NoteID entity is used to store the identifier.

#region NoteID
public abstract class noteID : PX.Data.IBqlField { }

[PXNote]
public virtual Guid? NoteID { get; set; }
#endregion

Additionally, you need to register and/or to allow the extension of the file that you want to be uploaded. For this use the File Upload Preferences (SM202550) form. Some regular extensions are prepared from the box. On this form, you may also need to define the maximum size of an uploaded file.

Acumatica uses two tables in DB to store an attachment: one table to store files in BIN format, second for information about screen that use this file. Thus, when you want to attach file a using code, you have to create records in both these tables.

Generally, you attach file in the next way:

GIST: https://gist.github.com/yuriycto/46eb4be2a9e26b0c9fc5e25a29383641

 

Then you may read the information about file like:

GIST: https://gist.github.com/yuriycto/64c62cd7ce76d4a16f5d6b6497a11a9a

If you need just to check if there was any file attached, you need to check length of attached files array:
var hasAttachedFiles = PXNoteAttribute.GetFileNotes(Document.Cache, Document.Current).Length != 0;

After a file was attached, it cannot be edited or updated. You may upload another file as new version. Or you may “exchange” file, that is to delete old file and upload a file with newer content.

You may also delete file from attachment:

GISThttps://gist.github.com/yuriycto/0772a453a6e70278bca6a9b1f3b017d9

Summary

The article explains how to attach, update, remove, and delete attachment files in Acumatica, using the Shipments screen as  an example. To enable support for data record attachments, the NoteID field should be added to DAC and the NoteID column with the unique identifier data type should be added to the database table. The File Upload Preferences form should also be used to register and allow the extension of the file to be uploaded, and to define the maximum size of the uploaded file. Acumatica uses two tables in the database to store attachments: one to store files in BIN format and another for information about the screen that uses the file. Code examples are provided for attaching, reading, and deleting files, and it is explained that once a file is attached, it cannot be edited or updated, but a new version can be uploaded, or the file can be exchanged by deleting the old file and uploading a new file with newer content.

Blog Author

Yuriy started programming in 2003 using C++ and FoxPro then switching to .Net in 2006. Beginning in 2013, he has been actively developing applications using the Acumatica xRP Framework, developing solutions for many clients over the years. He has a personal blog, aptly named Yuriy Zaletskyy’s Blog, where he has been documenting programming issues he’s run into over the past six years – sharing his observations and solutions freely with other Acumatica developers.
Categories: Developers

Receive blog updates in your Inbox.