Saturday, January 29, 2022

Import Picture or Attachment in Bulk in Business Central (From Zip Comparess File)

 Hello Friends,

 We develop the functionality to bulk import Attachment or Pictures on below master document

  • Customer
  • Vendor
  • Item
  • Fixed Asset

 There is two new action added on List Page for

  • Import Attachment Zip File
  • Import Image Zip File

 You need create one folder and put the image files where file name must be match with primary key of record (Like Customer No.)


compress this folder and make Zip File




Now come to respected list page and where you can see below action to impot it.




Above customer no. created for demo purpose where we going to bulk import image file

 After import zip system update picture on customer master !!!!












Same way we can import attachments again records.

 Technical Code Details : (for developer only !!!!)

===


pageextension 74981 CustomerListExtP74981 extends "Customer List"
{
    actions
    {
        addafter("Sent Emails")
        {
            action(ImportZipFile)
            {
                Caption = 'Import Zip File (Attachment)';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                Image = Import;
                ToolTip = 'Import Attachments from Zip';

                trigger OnAction()
                begin
                    ImportAttachmentsFromZip();
                end;
            }
            action(ImportPictureFile)
            {
                Caption = 'Import Zip File (Picture)';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                Image = Import;
                ToolTip = 'Import Picture from Zip';

                trigger OnAction()
                begin
                    LoadZIPPictureFile();
                end;
            }
        }
    }

    local procedure ImportAttachmentsFromZip()
    var
        FileMgt: Codeunit "File Management";
        DataCompression: Codeunit "Data Compression";
        TempBlob: Codeunit "Temp Blob";
        EntryList: List of [Text];
        EntryListKey: Text;
        ZipFileName: Text;
        Window: Dialog;
        FileName: Text;
        FileExtension: Text;
        InStream: InStream;
        EntryOutStream: OutStream;
        EntryInStream: InStream;
        Length: Integer;
        SelectZIPFileMsg: Label 'Select ZIP File';
        FileCount: Integer;
        Cust: Record Customer;
        DocAttach: Record "Document Attachment";
        NoCustError: Label 'Customer %1 does not exist.';
        ImportedMsg: Label '%1 attachments Imported successfully.';
        Cnt_lInt: Integer;
    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;

        Window.Open('#1##############################');

        //Loop files from the list type
        foreach EntryListKey in EntryList do begin
            Cnt_lInt += 1;
            Window.Update(1, Cnt_lInt);

            FileName := CopyStr(FileMgt.GetFileNameWithoutExtension(EntryListKey), 1, MaxStrLen(FileName));
            FileExtension := CopyStr(FileMgt.GetExtension(EntryListKey), 1, MaxStrLen(FileExtension));
            TempBlob.CreateOutStream(EntryOutStream);
            DataCompression.ExtractEntry(EntryListKey, EntryOutStream, Length);
            TempBlob.CreateInStream(EntryInStream);

            //Import each file where you want
            if not Cust.Get(FileName) then
                Error(NoCustError, FileName);

            DocAttach.Init();
            DocAttach.Validate("Table ID", Database::Customer);
            DocAttach.Validate("No.", FileName);
            DocAttach.Validate("File Name", FileName);
            DocAttach.Validate("File Extension", FileExtension);
            DocAttach."Document Reference ID".ImportStream(EntryInStream, FileName);
            DocAttach.Insert(true);
            FileCount += 1;
        end;

        //Close the zip file
        DataCompression.CloseZipArchive();
        Window.Close;

        if FileCount > 0 then
            Message(ImportedMsg, FileCount);
    end;

    procedure LoadZIPPictureFile(): Text
    var
        Item: Record Item;
        FileMgt: Codeunit "File Management";
        DataCompression: Codeunit "Data Compression";
        TempBlob: Codeunit "Temp Blob";
        Window: Dialog;
        EntryList: List of [Text];
        EntryListKey: Text;
        InStream: InStream;
        EntryOutStream: OutStream;
        EntryInStream: InStream;
        Length: Integer;
        FileName: Text;
        FileExtension: Text;
        Cust: Record Customer;
        NoCustError: Label 'Customer %1 does not exist.';
        ImportedMsg: Label '%1 attachments Imported successfully.';
        ZipFileName: Text;
        Cnt_lInt: Integer;
    begin
        if not UploadIntoStream(SelectZIPFileMsg, '', 'Zip Files|*.zip', ZipFileName, InStream) then
            Error('');

        DataCompression.OpenZipArchive(InStream, false);
        DataCompression.GetEntryList(EntryList);

        Window.Open('#1##############################');

        foreach EntryListKey in EntryList do begin
            Cnt_lInt += 1;
            Window.Update(1, Cnt_lInt);

            FileName := CopyStr(FileMgt.GetFileNameWithoutExtension(EntryListKey), 1, MaxStrLen(FileName));
            FileExtension := CopyStr(FileMgt.GetExtension(EntryListKey), 1, MaxStrLen(FileExtension));
            TempBlob.CreateOutStream(EntryOutStream);
            DataCompression.ExtractEntry(EntryListKey, EntryOutStream, Length);
            TempBlob.CreateInStream(EntryInStream);

            //Import each file where you want
            if not Cust.Get(FileName) then
                Error(NoCustError, FileName);

            Cust.Image.ImportStream(EntryInStream, FileMgt.GetFileName(EntryListKey));
            Cust.Modify();
        end;

        DataCompression.CloseZipArchive;
        Window.Close;

        exit(ZipFileName);
    end;

    var
        SelectZIPFileMsg: Label 'Select ZIP File';

}

I hope this help some one.

Keep Sharing....Keep Growing....

No comments:

Post a Comment