How to create attachments on the Location page and other pages in Business Central

In this post, I want to show a simple way to create the functionality of being able to attach documents in Business Central tables, which originally do not have this possibility, but thanks to the extensibility of the software we can achieve it.

In my previous post, I showed how to attach several documents contained in a .ZIP and I shared the list of all the tables masters that had the option to attach documents.

Quickly, before starting this simple post, I share those records that already have the option to attach documents.

  • Customer.
  • Vendor.
  • Item.
  • Employee.
  • Fixed Asset.
  • Resource.
  • Sales Header.
  • Purchase Header.
  • Job.
  • Sales Cr.Memo Header.
  • Sales Invoice Header.
  • Purch. Inv. Header.
  • Purch. Cr. Memo Hdr.
  • VAT Report Header.

This can be easily seen on “page 1174 “Document Attachment Factbox”

As we can see, for example, the Location record does not exist.

The idea of this post is to show not only how to create it for the Location record but any other that you want.

Codeunit

The following Codeunit has 2 events.

The Event OnAfterOpenForRecRef:

procedure OnAfterOpenForRecRef(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef)

It will be executed when the user clicks on the detail of the attachment as shown in the following image, initializing and associating the record Locations through its primary key “Code

Code:

The Event OnAfterInitFieldsFromRecRef:

procedure OnAfterInitFieldsFromRecRef(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef)

Similar to the previous event, this event will properly initialize the “Document Attachment” record but at insert time.

Code:

Page Extensions

Pageextension Document Attachment Factbox

The following “Document Attachment Factbox” page extension will allow us to send the information to the OpenForRecRef method with the proper reference, in this case, the Location record information.

Pageextension Location Card

The first part of this page extension shows the Factbox that was created for the Location Card Page.

In the second part, take advantage of the opportunity to include part of the development of the previous post, and be able to include .ZIP files.

If you saw the previous post, you will notice that I have slightly modified the line ImportCU.ImportAttachmentsFromZip(FromRecRef) since before it passed the corresponding record, but to make it more universal now it sent a RecordRef.

That’s all, a bit simple right?

Now, to extend to other tables, the only thing we have to replace is the Location record with the one we want, being careful with the primary key, since for the Location it was simply because the key is only one, but there may be the possibility of keys compound.

Video 1

The following video shows the operation that the Locations page did not have before.

Video 2

This video shows how it works to attach multiple documents contained in a .ZIP on the Locations page.

Conclusion

Thanks to the OnAfterOpenForRecRef and OnAfterInitFieldsFromRecRef events, we could easily extend the ability to attach documents to tables that were not originally created with that ability.

Additionally, we slightly modified the code to make the import of .ZIP more universal and easier when adapting it to any record.

Code on Github
All the code used in this post can be found at this link.

I hope this has been helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *