Thursday, August 7, 2008

import/export of chart of accounts by using X++ in microsoft Dynamics Axapta

Almost regularly I receive emails requesting for import/export jobs. Here are couple of simple examples. If you have any query, please do not hesitate to contact me..

1) Here is a small job that imports Chart of Accounts into Axapta. Please note that this job is meant more as a starting point for beginners in Axapta. Considering that, I have added some comments across the job.
------------------------------------------------------------------------
static void CoAImport(Args _args)
{

/*
*************************************************************************

Author : mouliyadav

Purpose : Demonstration for importing CoA into Axapta

*************************************************************************
*/

LedgerTable ldt;
AsciiIO inFile;
Filename filename;
Container line;
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
*/
}
------------------------------------------------------------------------


2) I wrote this simple job to demonstrate exporting data from Axapta. Also I have mentioned few file operations like copying into clipboard, deletion etc. Please note that I have used CommaIO class here. If required it is also possible to use class like AsciiIO etc. Here I go ....
static void FileOperations(Args _args)
{
/*
......................................................

Author : Harish Mohanbabu

Purpose : Demonstration for creating a text file and
writing contents into that file
......................................................
*/

CustTable cust;
CommaIO myfile;
TextBuffer tb;
counter recordCount;
int i;
str fileName;
;

fileName = "C:\\Axapta1.txt"; //<- Alternatively 'Dialog' can be used.
//<- Instead of '.Txt', extensions like
//.Doc, .Xls can also be used.

//Creating new file with write access
myfile = new commaIO(fileName,"w");


//Setting up Record delimiter
myfile.outRecordDelimiter("\r\n");
myfile.outFieldDelimiter(",");


while select cust order by accountnum
{
myfile.write(cust.AccountNum, cust.Name);
}

myfile.write('..........................................');


//To find out record count & write the same end of the file
recordcount = new sysdicttable(tablenum(custTable)).recordCount();
myfile.write("Inserted " + int2str(recordcount) + " Records");


//To close this file
myfile = null; //<- Finalize() method is protected. Hence this alternative ...


//To copy contents of this file into clipboard
tb = new TextBuffer();
tb.fromFile(filename);
tb.toClipboard();
tb = null;


//To delete this file
//winapi::deleteFile(fileName);


//Alternatively To move this file
//winapi::moveFile(fileName, newfileName); // <- Declare 'newfileName'
}


3) I wrote this simple job to demonstrate connecting to a website from Axapta and downloading a web page into the local hard disk. Here I go ....
static void ReadHTML(Args _args)
{
......................................................

Author : Harish Mohanbabu

Purpose : Demonstration for connecting to a website and
downloading a webpage from there....
......................................................

str page;
str filename;
commaIO myfile;
int handle;
WinInet wi = new WinInet();
;

filename = "C:\\HTMLfile.txt";
myfile = new commaIO(filename,"w");
handle = wi.internetOpenUrl('http://www.axaptabangalore.blogspot.com');

if (handle)
{
page = wi.internetReadFile(handle);
myfile.write(page);
}

wi.internetCloseHandle(handle);

}

No comments: