In the Stock Exchange Bookkeeping App I wanted to be able to attach multiple files (ex: PDFs) to BVB Entries.
The attachments are files received from the brokerage firm and they refer to a transaction. For example, if I buy 1 stock, I will have 3 BVB Entries with the same Transaction Number. If I attach files, they will be linked to the Transaction Number and visible as below, in a Factbox, for all BVB Entries with the same Transaction Number:
If I double click on a “File Name” in the Factbox, I have the possibility to Open or Save the file:
I also have 2 more options:
–> Delete an Existing File:
–> Upload a New File:
Let’s see now how I made this small functionality:
–> First, when we post a Journal, we need to create a Transaction No. for all Entries related to a transaction. I inspired from standard NAV Codeunit 12 and wrote some functions for this:
–> Then, because we can have multiple attachments linked to a Transaction Number, I created a new Table where I store the Attachments in a Blob field:
–> In this table we also have some functions:
* Function to View the file from the Blob field:
ViewBlob(BlobFieldNo : Integer;FileName : Text) RecRef.GETTABLE(Rec); FldRef := RecRef.FIELD(BlobFieldNo); IF FORMAT(FldRef.TYPE) <> 'BLOB' THEN EXIT; FldRef.CALCFIELD; TempBlob.Blob := FldRef.VALUE; IF NOT TempBlob.Blob.HASVALUE THEN ERROR('no value'); FileManagement.BLOBExport(TempBlob,FileName,TRUE);
* Function to Upload a new file as an Attachment:
InsertAttachment(SourceTableNo : Integer;SourceTransactionNo : Integer) IF UPLOADINTOSTREAM('','','',FileName,Instr) THEN BEGIN INIT; "Source Table No." := SourceTableNo; "Source Table Reference No." := SourceTransactionNo; "File Name" := GetFileName(FileName); Attachment.CREATEOUTSTREAM(OutStr); COPYSTREAM(OutStr, Instr); INSERT; END;
* Function to get the File Name from the File Path:
LOCAL GetFileName(FullFileName : Text) : Text FileNameWithoutPath := FullFileName; WHILE STRPOS(FileNameWithoutPath, '\') <> 0 DO FileNameWithoutPath:= COPYSTR(FileNameWithoutPath, 1 + STRPOS(FileNameWithoutPath, '\')); EXIT(FileNameWithoutPath);
–> Next step was to create a Factbox in which we display the attachments:
* Factbox has Source Table the Attachment table and one field “File Name” inside a repeater group:
* Inside the OnDrillDown trigger of field “File Name” of Factbox I call function “ViewBlob” from Attachment table. This is why, when we double click a File name in the Factbox, we can Open or Download the file:
File Name - OnDrillDown() ViewBlob(FIELDNO(Attachment),"File Name");
* I also used a global BVBEntry variable that is initialized in the FactBox page using a function:
SetBVBEntry(BVBEntry : Record "BVB Entry") GBVBEntry := BVBEntry;
* This global is Initialized and Changed when we change the current BVB Entry (OnAfterGetCurrRecord of page “BVB Entries”):
OnAfterGetCurrRecord() CurrPage.AttachFactbox.PAGE.SetBVBEntry(Rec);
* The Factbox is linked in the “BVB Entries” page in this way:
* Then, when we use Actions “New” and “Delete” in the Factbox, the following code is executed:
– Upload New Attachment:
AttachFile - OnAction() RecRef.GETTABLE(GBVBEntry); BVBAttachment.InsertAttachment(RecRef.NUMBER, GBVBEntry."Transaction No");
– Delete existing attachment:
DeleteFile - OnAction() DELETE;
I hope you ejoyed this post.