Logic given below for refence
Thanks in advance
var
FileMgt: Codeunit "File Management";
DataCompression: Codeunit "Data Compression";
TempBlob: Codeunit "Temp Blob";
EntryList: List of [Text];
EntryListKey: Text;
ZipFileName: Text;
FileName: Text;
FileExtension: Text;
InStream: InStream;
EntryOutStream: OutStream;
EntryInStream: InStream;
SelectZIPFileMsg: Label 'Select ZIP File';
FileCount: Integer;
DocAttach: Record "Document Attachment";
ImportedMsg: Label '%1 attachments imported successfully.';
TableID: Integer;
RecordNo: Code[20];
LineNo: Integer;
ID: Integer;
DocType: Enum "Purchase Document Type";
FileNameParts: List of [Text];
RecRef: RecordRef;
InvalidFormatErr: Label 'File name "%1" does not match expected format: {TableID}_{RecordNo}_{DocType}_{LineNo}_{ID}_{FileName}.{ext}';
RecordNotFoundErr: Label 'Record "%1" not found in table %2.';
InvalidValueErr: Label 'Invalid value "%1" for field %2.';
begin
// Upload zip file
if not UploadIntoStream(SelectZIPFileMsg, '', 'Zip Files|*.zip', ZipFileName, InStream) then
Error('');
DataCompression.OpenZipArchive(InStream, false);
DataCompression.GetEntryList(EntryList);
foreach EntryListKey in EntryList do begin
FileName := FileMgt.GetFileName(EntryListKey);
FileExtension := FileMgt.GetExtension(EntryListKey);
FileNameParts := FileName.Split('_'); // Use '_' as delimiter (adjust if needed)
if FileNameParts.Count < 6 then
Error(InvalidFormatErr, FileName);
if not Evaluate(TableID, FileNameParts.Get(1)) then
Error(InvalidValueErr, FileNameParts.Get(1), 'Table ID');
RecordNo := FileNameParts.Get(2); // Code[20] can be assigned directly
if not Evaluate(DocType, FileNameParts.Get(3)) then
Error(InvalidValueErr, FileNameParts.Get(3), 'Document Type');
if not Evaluate(LineNo, FileNameParts.Get(4)) then
Error(InvalidValueErr, FileNameParts.Get(4), 'Line No.');
if not Evaluate(ID, FileNameParts.Get(5)) then
Error(InvalidValueErr, FileNameParts.Get(5), 'ID');
FileNameParts.RemoveRange(1, 5); // Remove TableID, RecordNo, DocType, LineNo, ID
FileName := StrSubstNo('%1', StrJoin(FileNameParts, '_')); // Join remaining parts
RecRef.Open(TableID);
if not RecRef.GetBySystemId(RecordNo) then // Adjust based on primary key
Error(RecordNotFoundErr, RecordNo, TableID);
RecRef.Close();
Clear(EntryOutStream);
Clear(EntryInStream);
TempBlob.CreateOutStream(EntryOutStream);
if not DataCompression.ExtractEntry(EntryListKey, EntryOutStream) then
Error('Failed to extract file "%1".', FileName);
TempBlob.CreateInStream(EntryInStream);
if DocAttach.Get(TableID, RecordNo, DocType, LineNo, ID) then
DocAttach.Delete(true); // Optionally update instead of delete
DocAttach.Init();
DocAttach.Validate("Table ID", TableID);
DocAttach.Validate("No.", RecordNo);
DocAttach.Validate("Document Type", DocType);
DocAttach.Validate("Line No.", LineNo);
DocAttach.ID := ID;
DocAttach.Validate("File Name", FileName);
DocAttach.Validate("File Extension", FileExtension);
if not DocAttach."Document Reference ID".ImportStream(EntryInStream, FileName) then
Error('Failed to import file "%1" into Document Reference ID.', FileName);
DocAttach.Insert(true);
FileCount += 1;
end;
DataCompression.CloseZipArchive();
Message(ImportedMsg, FileCount);
end;
var
Result: Text;
Item: Text;
begin
foreach Item in List do begin
if Result = '' then
Result := Item
else
Result += Delimiter + Item;
end;
exit(Result);
end;