Writing data from a program to a text file using any programming language is a very common task for the programmers. It is also used while you are customizing or developing a new solution in Microsoft ERP systems such as Microsoft Dynamics 365 for Finance & Operations but it is pretty difficult to find out the code as this is not a very common task in ERP development.

It can be used in writing error logs, event logs, or data of different processes to a file to ship that data to someone else who needs data in text file with TSV format. Moreover, there are many and many different requirements that can be fulfilled using this code. Keeping that in mind, I have created this tutorial. Few days back, we have discussed about how to read data from TSV file using x++ in D365FO.

//Moeen Ahmed Sultan
//Microsoft Dynamics 365 for Finance & Operations
//Tel: +92 321 458 9595
//Email: moeenahmedsultan@hotmail.com

class WriteDataToTSVFileJob
{        
    public static void main(Args _args)
    {  
        TextIo           file;
        str              fileName;
        FileIOPermission IOPermission;
        container        cont;
        #File
        ;
        try
        {
            fileName = "C:\\TSVDataFile.txt";
            IOPermission = new FileIOPermission(fileName, #IO_APPEND);//If you want to write after deleting previous data, use #IO_WRITE instead of #IO_APPEND
            IOPermission.assert();
            file = new TextIo(filename, #IO_APPEND);//If you want to write after deleting previous data, use #IO_WRITE instead of #IO_APPEND
            file.outRecordDelimiter(#delimiterCRLF);
            file.outFieldDelimiter("\t");
        
            //It will create one tab separated row/line in the text file.
            cont = connull();
            cont = conins(cont, 1, "Moeen");
            cont = conins(cont, 2, "Ahmed");
            cont = conins(cont, 3, "Sultan");               
            file.writeExp(cont);
            
        }
        catch(Exception::Error)
        {
            error("ERROR");
        }
        CodeAccessPermission::revertAssert();
    }
}

Note: The disk partition of your computer should have allowed write permissions in order to run this code.

I have run this job three time and the text file is as follows:

Note: If you found any ambiguity or a better solution, please comment.

Data file: TSVDataFile – Output

GitHub: Click here

YouTube Channel: Click here

Strong people don’t put others down… They lift them up. ― Michael P. Watson

Share on Facebook
Share on LinkedIn
Share on Twitter
Share on Google+
Share on Skype

The post Writing data to TSV file using x++ in D365FO appeared first on NevoiTech Blog.