Friday 9 January 2015

Code to write a text file using X++

Many  time we came across situation to where we need to import data in text file.Here is a simple way to write the data into a text file.
static server void WriteTextFile(Args _args)
{ TextIo file; // Using the @ before the filename // enables us to use single path // delimiters. If you don't use it // you will have to write the path like this: FileName filename = @"c:\<<Filename.txt>>"; CarTable carTable; container con; FileIoPermission permission; #File ; try {
// Create the permission class
permission = new FileIoPermission(filename, #io_write);
// Add a request for permission before new TextIo()
permission.assert();
// Create the TextIo object
file = new TextIo(filename, #io_write);
if (!file)
throw Exception::Error;
// Specify the delimiters
file.outRecordDelimiter(#delimiterCRLF);
file.outFieldDelimiter(";");
// Loop through the data source
while select carTable
{
// Empty the container
con = connull();
// Set the data into the container
con += carTable.CarId;
con += carTable.CarBrand;
con += carTable.Mileage;
con +=carTable.Model;
con += carTable.ModelYear;
// Write the container to the file
file.writeExp(con);
}
}
catch(Exception::Error)
{
error("You do not have access to write the file to the
selected folder");
}
// Revert the access privileges
CodeAccessPermission::revertAssert();
}

No comments:

Post a Comment