Hi Everyone, 
Step-(2) Secondly based on the data format and columns, create a new Procedure/function on Codeunit to choose and import file using the dialog box to read the data.
Thanks for Reading!!
In this article we will see how we can Import Data (CSV/Excel format) in Microsoft Dynamics 365 Business Central.
Here i am using CSV Buffer as a record variable to read data from csv file for temporary basis and then will store it to Master table.
Let us take a scenario to import CSV file to Employee Table.
Step-(1) First analyze the format/ structure of the csv data file as shown below:
Step-(2) Secondly based on the data format and columns, create a new Procedure/function on Codeunit to choose and import file using the dialog box to read the data.
codeunit 50001 "CSV Import"
{
    trigger OnRun()
    begin
    end;
    procedure ImportEmployeeDetailViaCSVBuffer()
    var
        csv_InStream: InStream;
        uploadResult: Boolean;
        csvFileName: Text;
        csvBuffer: Record "CSV Buffer";
        RecEmployee: Record Employee;
    begin
        if UploadIntoStream('Import Employee CSV', '', '', csvFileName, csv_InStream) then begin
            csvBuffer.DeleteAll();
            csvBuffer.LoadDataFromStream(csv_InStream, ',');
            if csvBuffer.FindSet() then begin
                repeat
                    if csvBuffer."Line No." > 1 then begin // To Ignore 1st Line (Header Part)
                        if (csvBuffer."Field No." = 1) then
                            RecEmployee.Init();
                        case csvBuffer."Field No." of
                            1:
                                begin
                                    RecEmployee.Validate("No.", csvBuffer.Value);
                                end;
                            2:
                                begin
                                    RecEmployee."First Name" := csvBuffer.Value;
                                end;
                            3:
                                begin
                                    RecEmployee."Middle Name" := csvBuffer.Value;
                                end;
                            4:
                                begin
                                    RecEmployee."Last Name" := csvBuffer.Value;
                                end;
                            6:
                                begin
                                    RecEmployee."Job Title" := csvBuffer.Value;
                                end;
                            7:
                                begin
                                    RecEmployee."Phone No." := csvBuffer.Value;
                                end;
                            8:
                                begin
                                    RecEmployee."Search Name" := csvBuffer.Value;
                                end;
                            9:
                                begin
                                    RecEmployee.Address := csvBuffer.Value;
                                    RecEmployee.Insert();
                                end;
                        end;
                    end;
                until csvBuffer.Next() = 0;
                Message('Imported Successfully.');
            end;
        end;
    end;
}
Step-(3) Now create new Employee List Page Extension, and add new action button and call the required procedure:
pageextension 50001 "Employee List Ext" extends "Employee List"
{
    actions
    {
        // Add changes to page actions here
        addlast(Processing)
        {
            action("Import Employee Via CSV")
            {
                ApplicationArea = All;
                PromotedCategory = Process;
                Promoted = true;
                PromotedIsBig = true;
                Image = ImportExcel;
                trigger OnAction()
                Var
                    CSVImport_CU: Codeunit "CSV Import";
                begin
                    CSVImport_CU.ImportEmployeeDetailViaCSVBuffer();
                end;
            }
        }
    }
}
Output- Open Employee List Page and click on action button "Import Employee Via CSV"  and Choose the file and click on OK button:
Happy to Help :)

I don't know why, but I can not skip the header
ReplyDeleteYou need to add condition to skip Header Line, see below Code : if csvBuffer."Line No." > 1 then begin // To Ignore 1st Line (Header Part)
ReplyDelete