Tuesday, January 17, 2023

Import CSV file using x++ in D365FO

 In this post we can see how to import CSV file using x++. I have done by using RunBase batch. Because user will upload more data and he want to run in batch.

Dialog:


Using RunBase Batch

class TerminationBatch extends RunBaseBatch
{
    Filename        filename;
    dialog          dialog;

    #define.CurrentVersion(1)
    #define.Version1(1)
    #localmacro.CurrentList
        fileName
    #endmacro

    client server static ClassDescription description()
    {
        return 'Upload CSV file';
    }

    protected boolean canRunInNewSession()
    {
        return false;
    }


    public Object dialog()
    {
        DialogGroup         dialogGroup;
        FormBuildControl    formBuildControl;
        FileUploadBuild     dialogFileUpload;
       // Set              enumSet = new Set(Types::Enum);
                
        dialog              = super();
        dialogGroup         = dialog.addGroup('File picker');
        formBuildControl    = dialog.formBuildDesign().control(dialogGroup.name());
        
        dialogFileUpload = formBuildControl.addControlEx(classstr(FileUpload), filename);
        dialogFileUpload.style(FileUploadStyle::MinimalWithFilename);
        dialogFileUpload.baseFileUploadStrategyClassName(classstr(FileUploadTemporaryStorageStrategy));
        dialogFileUpload.fileTypesAccepted('.csv');
        dialogFileUpload.fileNameLabel('Select worker data file');
    
        return dialog;
    }

    static void main(Args _args)
    {
        TerminationBatch objClass = new TerminationBatch();

        if (objClass.prompt())
        {
            objClass.runOperation();
        }
    }

    public void run()
    {
        #File
        container                           currentLine;
        CommaTextStreamIo                   localStream;
        str                                 textFile;
       
        FileUpload fileUploadControl = this.getFormControl(dialog, filename);
        
        FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

        if (fileUploadResult != null && fileUploadResult.getUploadStatus())
        {
            textFile = fileUploadResult.getDownloadUrl();
        }
        
        localStream = CommaTextStreamIo::constructForRead(File::UseFileFromURL(textFile));
       
          
        if (localStream.status() != IO_Status::Ok)
        {
            throw error(strfmt('Is not possible to open the file. Error %1',enum2str(localStream.status())));
        }
    
        localStream.inFieldDelimiter(',');
        while (localStream.status() ==  IO_Status::Ok)
        {
            currentLine = localStream.read();

            if (!currentLine)
            {
                break;
            }
            try
            {

                Id    = conPeek(currentLine, 1);
                Date  = conPeek(currentLine, 2);
				// Remaining fields             
            }
        
            catch (Exception::Error)
            {
                Throw (Exception::Error);
            }
        }
        info('Success');
    }

    protected FormControl getFormControl(DialogRunbase dialog, str controlName)
    {
        return dialog.formRun().control(_dialog.formRun().controlId( controlName));
    }

}


Using Job.

        AsciiStreamIo                                   file;
        Array                                           fileLines;
        FileUploadTemporaryStorageResult                fileUpload;
        fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
        file = AsciiStreamIo::constructForRead(fileUpload.openResult());

        if (file)
        {
            if (file.status())
            {
                throw error("@SYS52680");
            }

            file.inFieldDelimiter(',');
            file.inRecordDelimiter('\r\n');
        }

        container record;
        while (!file.status())
        {
            record = file.read();
            if (conLen(record))
            {
                info(strFmt("%1 - %2",conPeek(record,1),conPeek(record,2)));
            }
        }

Keep Daxing!! 

No comments:

Post a Comment