Upload utility: upload excel and import data using file upload (X++)



In this blog, we will discuss on upload excel file using file upload utility

Sometime we have requirement to upload excel file and import data using X++ using manual upload.

Below are the code to achieve this development:

public class XXXImportEventHandle
{
    [FormControlEventHandler(formControlStr(SalesTable, salesTable), FormControlEventType::Clicked)]
    public static void SalesUpload_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        System.IO.Stream stream;


        OfficeOpenXml.ExcelWorksheet worksheet;
        OfficeOpenXml.ExcelRange     range;
        FileUploadTemporaryStorageResult fileUploadResult;
        FileUploadBuild  fileUpload;
        DialogGroup      dlgUploadGroup;
        FileUpload       fileUploadControl;
        FileUploadBuild  fileUploadBuild;
        FormBuildControl formBuildControl;
	ExcelSpreadsheetName sheeet;
        int              i, excelRowCount;


        Dialog dialog = new Dialog("Upload sales order excel file");
        dlgUploadGroup = dialog.addGroup("Upload file");
        formBuildControl = dialog.formBuildDesign().control(dlgUploadGroup.name());
        fileUploadBuild = formBuildControl.addControlEx(classstr(FileUpload), 'Upload');
        fileUploadBuild.fileTypesAccepted('.xlsx');  // Upload excel file only
        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
        if (dialog.run() && dialog.closedOk())
        {
            fileUploadControl = dialog.formRun().control(dialog.formRun().controlId('Upload'));
            fileUploadResult  = fileUploadControl.getFileUploadResult();


            if (fileUploadResult != null && fileUploadResult.getUploadStatus())
            {
                stream = fileUploadResult.openResult();
                using (OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(stream))
                {
                    Package.Load(stream);
                    worksheet = package.get_Workbook().get_Worksheets().get_Item(1);
                    range     = worksheet.Cells;
                    excelRowCount = worksheet.Dimension.End.Row - worksheet.Dimension.Start.Row + 1;
                    try
                    {
                        for (i = 2; i<= excelRowCount; i++)  // Start with 2 due to header excluded.
                        {
                            salesTable                 = XXXImportEventHandler::findSalesTable(range.get_Item(i, 1).value);
                            salesTable.SalesId         = range.get_Item(i, 1).value;
                            salesTable.CustAccount     = range.get_Item(i, 2).value;
			    // Sample code
                            if(salesTable.RecId)
                            {
                                ttsbegin;
                                salesTable.update();  // To update existing record
                                ttscommit;
                            }
                            else
                            {
                                salesTableInsert = salesTable;
                                salesTableInsert.Insert();   // For new record
                            }
                        }
                        info("Data inserted succesfully");
                    }
                    catch (Exception::Error)
                    {
                        ttsabort;
                        throw error("error during upload");
                    }
                }
            }
        }
    }


    
    public static SalesTable findSalesTable(XXXX _xxx)
    {
        SalesTable salesTable;


        select salesTable
		where salesTable.Salesid== _xxx;
         
        
        if(salesTable.RecId)
            salesTable.selectForUpdate(true);
        
        return salesTable;
    }


}





Comments

Popular posts from this blog

Execute D365 F&O SSRS report with parameter and upload report output on Azure blob storage using X++

Microsoft D365 F&O: Remove custom models from deployable package

Generate Text/CSV/DAT file using X++ and upload on Azure blob storage