Tuesday, November 6, 2018

X++ | d365FO - import and export csv file (Part 2 - Import)

I researched and found many interesting csv posts, then I rewrite it to keep it here in this blog. You will find all references at the bottom.

From the part 1 https://shootax.blogspot.com/2018/11/x-d365fo-import-and-export-csv-file.html, here the scenario is still simple. We will import that csv file and display the data.


Solution



































class Example_Job_CSVimport
{        
    public static void main(Args _args)
    {  
        FileUploadTemporaryStorageResult importFile;
        container record;

        importFile = File::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy));

        if(importFile && importFile.getUploadStatus())
        {
            CommaStreamIo io = CommaStreamIo::constructForRead(importFile.openResult());

            if (io)
            {
if (io.status())
throw error('@SYS52680');

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

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

    }


}


When running


















Explanation

These below are the types/classes we used to transfer data.

File --> CommaStreamIO --> Container


I hope the example is simple and let you see the idea behind. Don't forget to check the original post if you need to know more details about it. Until the next post!

Ref: http://axrachit.blogspot.com/2017/02/x-code-to-read-csv-files-in-dynamics.html

No comments:

Post a Comment