/ Microsoft / Reading data from TSV file using x++ in D365FO

Reading data from TSV file using x++ in D365FO

Learn, share and spread knowledge

Sometimes, it is required to read the data from the text files to insert in to the system. Reading data from text file using x++ is simple to code but logical. I have created this job which reads the data from the TSV (tab-separated file) and prints it on the screen. You can customize this generic job to meet your own requirements. You can also use it to read comma separated file. See the code below:

class ReadDataFromTSVFileJob
{        
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        TextIo file;
        FileName filename;
        container cont;
        FileIoPermission permission;
        boolean headerRow;
        #File
        
        try
        {
            //File name along with complete path
            filename = @"C:\TSVDataFile.txt";
            permission = new FileIoPermission(filename, #io_read);
            permission.assert();
            file = new TextIo(filename, #io_read);
            if (!file)
            throw Exception::Error;
            file.inRecordDelimiter(#delimiterCRLF);
            //inFieldDelimiter function splits the data where TAB is found because \t is passed as parameter and \t means tab.
            file.inFieldDelimiter("\t");           
            cont = file.read();
            //if headerRow is true, then it will not print the first line which is usually consists of Headings.
            headerRow = true;
            while (file.status() == IO_Status::Ok)
            {
                if(headerRow)
                {
                    headerRow=false;
                }
                else
                {
                    info(strfmt("%1 %2 %3 %4 %5 %6 %7 %8 %9",conpeek(cont,1),conpeek(cont,2),conpeek(cont,3),conpeek(cont,4),conpeek(cont,5),conpeek(cont,6),conpeek(cont,7),conpeek(cont,8),conpeek(cont,9)));
                }
                cont = file.read();
            }
        }
        catch(Exception::Error)
        {
            error("ERROR");
        }
        CodeAccessPermission::revertAssert();
    }
}

Data file: TSVDataFile

Code: Click here

Also, see the article about Writing data to text file using x++.

Reading data from text file using x++ is well-explained above. If you found any ambiguity or a better solution, please let me know. If this helps you, please Like, Comment and Share to help other community members.

Blog: Click here

YouTube: Click here

GitHub: Click here

Don’t compare yourself with anyone in this world…if you do so, you are insulting yourself. – Bill Gates

2 POST COMMENT

Send Us A Message Here

Your email address will not be published. Required fields are marked *

2 Comments