Hi Friends,
[Version: NAV 2016]
I need to IMPORT/EXPORT/DELETE attachment and found a solution by browsing on internet. Below coding is working fine except for exporting multiple files for a record. I have given entire working code to show what I did.
New Table: Document Attachment Table with below fields.
1. Primary Key (RecordID)
2. Attachment (BLOB)
3. Extension (Text 30)
4. Document No. (Code 20)
5. Name of the File (Text 50)
6. Line No. (Integer)
Table: Global Variables
Filename (Text 1024)
ServerFilename (Text 1024)
FileManagement (Codeunit - File Management)
ExtensionStart (Integer)
TempBlob (Record - TempBlob)
Table: Text Constants
Text001 Do you want to overwrite existing attachment?
Text002 Import attachment.
Text003 Update attachment.
Text004 No attachment in current document.
Text005 Attachment File %1 imported successfully.
Text006 Do you want to delete the attachment in %1?
Table: Functions
ImportAttachment
ExportAttachment
DeleteAttachment
UpdateAttachment
RecordExist
ImportAttachment(RecIDToImport : RecordID)
IF RecordExist(RecIDToImport) THEN
EXIT;
INIT;
"Line No." := "Line No." + 10000;
"Primary Key" := RecIDToImport;
Filename := FileManagement.BLOBImportWithFilter(TempBlob,Text002,'','*.*|','*.*');
IF Filename = '' THEN
EXIT;
Attachment := TempBlob.Blob;
"Name of the File" := FileManagement.GetFileName(Filename);
Extension := '.' + FileManagement.GetExtension(Filename);
INSERT;
IF Attachment.HASVALUE THEN
MESSAGE(Text005,Filename);
ExportAttachment(RecIDToExport : RecordID;RecLineNo : Integer;RecNameofFile : Text)
SETRANGE("Primary Key",RecIDToExport);
SETRANGE("Line No.",RecLineNo);
SETRANGE("Name of the File",RecNameofFile);
IF NOT FINDFIRST THEN
ERROR(Text004);
CALCFIELDS(Attachment);
IF NOT Attachment.HASVALUE THEN
ERROR(Text004);
TempBlob.Blob := Attachment;
FileManagement.BLOBExport(TempBlob,FORMAT('*' + Extension + ''),TRUE);
DeleteAttachment(RecIDToDelete : RecordID)
SETRANGE("Primary Key",RecIDToDelete);
IF NOT FINDFIRST THEN
ERROR(Text004);
CALCFIELDS(Attachment);
IF Attachment.HASVALUE THEN
IF CONFIRM(Text006,FALSE,RecIDToDelete) THEN
DELETE;
UpdateAttachment(VAR RecIDToUpdate : RecordID)
CALCFIELDS(Attachment);
IF NOT CONFIRM(Text001) THEN
EXIT;
Filename := FileManagement.BLOBImportWithFilter(TempBlob,Text002,'','*.*|','*.*');
IF Filename = '' THEN
EXIT;
Attachment := TempBlob.Blob;
"Name of the File" := FileManagement.GetFileName(Filename);
Extension := '.' + FileManagement.GetExtension(Filename);
MODIFY;
IF Attachment.HASVALUE THEN
MESSAGE(Text005,Filename);
RecordExist(RecIDToCheck : RecordID) : Boolean
SETRANGE("Primary Key",RecIDToCheck);
IF NOT FINDFIRST THEN
EXIT(FALSE);
UpdateAttachment(RecIDToCheck);
EXIT(TRUE);
I am calling the functions from Page (action pane) and those codings are given below.
Page - Global Variables
AttachmentManagement (Record - Document Attachment Table)
AttachmentRecRef (RecordRef)
AttachmentRecID (RecordID)
Page Functions
ImportAttachment - OnAction()
AttachmentRecRef.OPEN(DATABASE::"Purch. Request Header");
AttachmentRecRef.SETPOSITION(GETPOSITION);
AttachmentRecID := AttachmentRecRef.RECORDID;
AttachmentRecRef.CLOSE;
AttachmentManagement.ImportAttachment(AttachmentRecID);
AttachmentManagement."Document No." := "No.";
AttachmentManagement.MODIFY;
ExportAttachment - OnAction()
AttachmentRecRef.OPEN(DATABASE::"Purch. Request Header");
AttachmentRecRef.SETPOSITION(GETPOSITION);
AttachmentRecID := AttachmentRecRef.RECORDID;
AttachmentRecRef.CLOSE;
AttachmentManagement.ExportAttachment(AttachmentRecID);
DeleteAttachment - OnAction()
AttachmentRecRef.OPEN(DATABASE::"Purch. Request Header");
AttachmentRecRef.SETPOSITION(GETPOSITION);
AttachmentRecID := AttachmentRecRef.RECORDID;
AttachmentRecRef.CLOSE;
AttachmentManagement.DeleteAttachment(AttachmentRecID);
When I tried to import files (multiple files), it is properly importing. But when I tried to export, only first attached file is exporting. Say when I try to export the third attachment for a record, the first attachment is only opening.
I believe it is a minor one but got struck up. Can anyone help how to solve this?
Thanks in advance.