Tuesday 24 December 2013

Write data to a text file through X++ in ax 2012

static void WriteTextfile(Args _args)
{
    CustTable   custTable;
    BinData     binData;
    TextBuffer  textBuffer;
    ;

    textBuffer = new TextBuffer();
    textBuffer.setText('');

    while select custTable where custTable.AccountNum < '40020'
    {
        textBuffer.appendText(strfmt('%1\r\n',custTable.AccountNum));
    }

    textBuffer.getText();

    binData = new BinData();
    binData.setStrData(textBuffer.getText());
    binData.saveFile(@"d:\iba.txt");

}
________________________________________________________________________________
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:
// "c:\\temp\\cars.txt"
FileName filename = @"c:\temp\cars.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 = conins(con, 1, carTable.CarId);
con = conins(con, 2, carTable.CarBrand);
con = conins(con, 3, carTable.Mileage);
con = conins(con, 4, carTable.Model);
con = conins(con, 5, 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();
}
(OR)
while select carTable
{
// Write the data to the file
file.write(carTable.CarId,
carTable.CarBrand,
carTable.Mileage,
carTable.Model,
carTable.ModelYear);
}

No comments:

Post a Comment