Friday, December 19, 2008

importing chart of accounts from CSV file

static void CoAImport(Args _args)
{
LedgerTable ldt;
AsciiIO inFile;
Filename filename;
Container line;
str tmp1, tmp2, tmp3;
Dialog dialog;
DialogField dialogField;
;

/*
Note - In this example, I have used 'Dialog' & DialogField classes
as a tool for me to select the file that has to be imported. If required, you can directly select the file to be imported. For ex -
filename = 'C:\\';
*/

dialog = new Dialog("Import Chart of Accounts");
dialogfield = dialog.addField(typeid(Filenameopen), "File Name");
dialog.run();

if (dialog.run())
{
  filename = (dialogfield.value());
}

/*
Note - In this example, I have used AsciiIO class. But you can also use
CommaIO class as well. Basically all these classes (AsciiIO, CommaIO,
Comma7Io etc) derives from 'Io' base class. For more info, please
refer to Io class.
*/
inFile = new AsciiIO (filename, 'R');

if (!inFile || infile.status() != IO_Status::Ok )
{
   //strfmt - function for formatting text string
   throw error (strfmt("@SYS19312",filename));
}
ttsbegin;

infile.inRecordDelimiter('\n');
infile.inFieldDelimiter(',');

//Checking status of last operation..
while (infile.status() == IO_status::Ok)
{
   line = infile.read();

   if (line)
   {
     ldt.initValue();
     ldt.AccountNum = conpeek(line,1);
     ldt.AccountName = conpeek(line,2);
     ldt.AccountNameAlias = conpeek(line,3);
     ldt.AccountPlType = conpeek(line,4);
     ldt.doInsert();
   }
}
ttscommit;

/*
Some additional notes:

1) 'line' is a container. It is a general data type. Its purpose is very similar to temporary tables. However there are some subtle yet great difference between temp tables and containers. For more info, please refer to Developer's guide.

2) Conpeek - To return specific element from a container
*/
}

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...