I have one zip file with multiple pdf and and i want to attached those pdf files properly. Currently attachment are inserting with all corrects details including primary keys but when we open pdf file it show some random file details with some blank pages.
Logic given below for refence
Thanks in advance
local procedure ImportAttachmentsFromZip()
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('');
// Extract zip file and store files to list type
DataCompression.OpenZipArchive(InStream, false);
DataCompression.GetEntryList(EntryList);
FileCount := 0;
// Loop files from the list type
foreach EntryListKey in EntryList do begin
FileName := FileMgt.GetFileName(EntryListKey);
FileExtension := FileMgt.GetExtension(EntryListKey);
// Expecting file name format: {TableID}_{RecordNo}_{DocType}_{LineNo}_{ID}_{FileName}.{ext}
FileNameParts := FileName.Split('_'); // Use '_' as delimiter (adjust if needed)
if FileNameParts.Count < 6 then
Error(InvalidFormatErr, FileName);
// Parse and validate fields with error handling
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');
// Reconstruct the file name (handle underscores in the file name)
FileNameParts.RemoveRange(1, 5); // Remove TableID, RecordNo, DocType, LineNo, ID
FileName := StrSubstNo('%1', StrJoin(FileNameParts, '_')); // Join remaining parts
// Verify the record exists
RecRef.Open(TableID);
if not RecRef.GetBySystemId(RecordNo) then // Adjust based on primary key
Error(RecordNotFoundErr, RecordNo, TableID);
RecRef.Close();
// Extract the file content
Clear(EntryOutStream);
Clear(EntryInStream);
TempBlob.CreateOutStream(EntryOutStream);
if not DataCompression.ExtractEntry(EntryListKey, EntryOutStream) then
Error('Failed to extract file "%1".', FileName);
TempBlob.CreateInStream(EntryInStream);
// Check for existing attachment
if DocAttach.Get(TableID, RecordNo, DocType, LineNo, ID) then
DocAttach.Delete(true); // Optionally update instead of delete
// Create new document attachment
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;
// Close the zip file
DataCompression.CloseZipArchive();
if FileCount > 0 then
Message(ImportedMsg, FileCount);
end;
local procedure StrJoin(List: List of [Text]; Delimiter: Text): Text
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;