Importing records from excel using X++ code

 we are trying to import data from Excel to D365FO through the following job.

Create a job :-

using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.ExcelPackage;

using OfficeOpenXml.ExcelRange;

class BASEmployeeDataImportJob

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    

    public void run()

    {

        this.updateDailyAttendance();

    }


    void updateDailyAttendance()

    {

        System.IO.Stream                stream;

        ExcelSpreadsheetName            sheeet;

        FileUploadBuild                 fileUpload;

        DialogGroup                     dlgUploadGroup;

        FileUploadBuild                 fileUploadBuild;

        FormBuildControl                formBuildControl;

        BAS_Employees                   emplTimeAttendance, insertTimeAttendance, updateTimeAttendance;

        COMVariantType                  type;

        Dialog              dialog =    new Dialog("Employee data importing");


        dlgUploadGroup          = dialog.addGroup('@SYS54759');

        formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name());

        fileUploadBuild         = formBuildControl.addControlEx(classstr(FileUpload), 'Upload');

        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);

        fileUploadBuild.fileTypesAccepted('.xlsx');


        str COMVariant2Str(COMVariant _cv)

        {

            switch (_cv.variantType())

            {

                case COMVariantType::VT_BSTR:

                    return _cv.bStr();

                case COMVariantType::VT_EMPTY:

                    return '';

                default:

                    throw error(strfmt('@SYS26908', _cv.variantType()));

            }

        }


    //    if (dialog.run() && dialog.closedOk())

      //  {

        //    FileUpload fileUploadControl                      = dialog.formRun().control(dialog.formRun().controlId('Upload'));

            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

                 

            if (fileUploadResult != null && fileUploadResult.getUploadStatus())

            {

                stream = fileUploadResult.openResult();

                using (ExcelPackage Package = new ExcelPackage(stream))

                {

                    int  rowCount, i,columncount,j;

                    Package.Load(stream);

                    ExcelWorksheet   worksheet          = package.get_Workbook().get_Worksheets().get_Item(1);

                    OfficeOpenXml.ExcelRange    range   = worksheet.Cells;

                    rowCount                            = (worksheet.Dimension.End.Row) - (worksheet.Dimension.Start.Row)  + 1;

                    columncount                         = (worksheet.Dimension.End.Column);


                    for (i = 2; i<= rowCount; i++)

                    {

                        str Emplid;

                        emplid                          = (range.get_Item(i, 1).value);

                      

                        select * from emplTimeAttendance

                            where emplTimeAttendance.BAS_EmployeID == Emplid;

                        //if record already exists update it

                        if(emplTimeAttendance)

                        {

                            ttsbegin;

                            emplTimeAttendance.selectForUpdate(true);

                            emplTimeAttendance.BAS_EmployeID            =  any2Str(range.get_Item(i, 1).value);

                            emplTimeAttendance.BAS_FirstName            =  any2Str(range.get_Item(i, 2).value);

                            emplTimeAttendance.BAS_Address              =  any2Str(range.get_Item(i, 3).value);

                            emplTimeAttendance.BAS_City                 =  any2Str(range.get_Item(i, 4).value);

                            emplTimeAttendance.BAS_BirthDate            =  any2Date(range.get_Item(i, 5).value);

                            emplTimeAttendance.BAS_HireDate             =  any2Date(range.get_Item(i, 6).value);

                            emplTimeAttendance.update();

                            ttscommit;

                        }

                        //insert the new record

                        Else

                        {

                            insertTimeAttendance.BAS_EmployeID          = (range.get_Item(i, 1).value);

                            insertTimeAttendance.BAS_FirstName          = any2Str(range.get_Item(i, 2).value);

                            insertTimeAttendance.BAS_Address            = any2Str(range.get_Item(i, 3).value);

                            insertTimeAttendance.BAS_City               = any2Str(range.get_Item(i, 4).value);

                            insertTimeAttendance.BAS_BirthDate          = any2Date(range.get_Item(i, 5).value);

                            insertTimeAttendance.BAS_HireDate           = any2Date(range.get_Item(i, 6).value);

                            insertTimeAttendance.insert();

                        }


                    }


                }


            }


            else

            {

                error('Error here');

            }

        }

    }


    public static void main(Args _args)

    {

        BASEmployeeDataImportJob     employeeDetailsImport;

        employeeDetailsImport      = new BASEmployeeDataImportJob ();

        employeeDetailsImport.run();

       

    }


}

Comments

Popular posts from this blog

How to Create a wizard in x++ d365

X++ code to CREATE AND POST A TRADE AGREEMENT IN MICROSOFT DYNAMICS 365 in D365 F & O

x++ code to submit and approve and reject the invent movement workflow in d365 F&O