Writing data to TSV file using x++ in D365FO
Writing data from a program to a text file using any programming language is a very common task for the programmers. Writing data to text file using x++ is simple but logical. 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.
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:
Data file: TSVDataFile – Output
Code: Click here
Also, see the article about Reading data from text file using x++.
Writing data to 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
Strong people don’t put others down… They lift them up. ―
Pingback:Reading data from TSV file using x++ in D365FO | NevoiTech Blog