Tuesday 8 April 2008

Import items into InventTable and associated tables

Yesterday I needed to import item data into a company for testing purposes, so I wrote a small job which reads an csv-file containing itemId and itemName and creates the posts in InventTable and its associated tables, InventTableModule and InventItemLocation

static void InventTableImport(Args _args)

{
//
//Author : Steffen Denize
//Purpose : Demonstration for importing InventTable into Dynamics AX
//
InventTable inventTable;
CommaIO inFile;
Filename filename;
Dialog dialog;
DialogField dialogField;
ItemId itemId;
int counter;
InventTableModule inventTableModuleBuf;
InventItemLocation inventItemLocationBuf;
ItemName itemName;
;

//
// Note - In this example, I have used 'Dialog' & DialogField classes
// as a tool for me to select the file that has to be imported.
//
dialog = new Dialog("Demo of import of items into a blank InvenTable");
dialogfield = dialog.addField(typeid(Filenameopen), "File Name");
dialog.run();

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

//
// Note - In this example, I have used CommaIO class. But you can also use
// AsciiIO 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 CommaIO (filename, 'R');

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

// Set the delimiters
infile.inFieldDelimiter(';');
infile.inRecordDelimiter('|');

//Checking status of last operation
while (infile.status() == IO_status::Ok)
{
// Setting container according to datatypes
[itemId, itemName] = infile.read();
if (itemId)
{
if(!InventTable::find(itemId).RecId)
{
inventTable.initValue();
inventTable.ItemId = itemId;
inventTable.ItemName = itemName;
// These values must be set on their respective tables.
// Here the value are hard-coded into the import as the import
// serves a demo purpose only
inventTable.ItemGroupId = 'TST';
inventTable.ModelGroupId = 'TST';
inventTable.DimGroupId = 'TST';
inventTable.Insert();

// Values must be setup in inventTableModule, which in Dynamics AX 4.0SP2 has three valid arrayelements
// purch, sales, invent (DEL_smmQuotation is not included in this job)
for(counter = 0; counter <= 2; counter++)
{
inventTableModuleBuf.ItemId = inventTable.ItemId;
inventTableModuleBuf.ModuleType = counter;
inventTableModuleBuf.insert();
}

InventItemLocationBuf.ItemId = inventTable.ItemId;
InventItemLocationBuf.inventDimId = InventDim::inventDimIdBlank();
InventItemLocationBuf.insert();
}
}
}
ttscommit;
}

Please notice that the posting is provided "as is" with no warranties and confers no rights

3 comments:

Chandra Setiawan said...

thanks men..you save my time :)

Anonymous said...

hI,

Thank you for sharing. I do not exactly know why table InventTableModuleBuf is required. Can you explain that to me?

Instead of a counter, what tablenum() can be used instead?

J.

Anonymous said...

ModuleInventPurchSales::Invent;
answer already found. sorry for interrupting you.

Do you know how I can make a dialog asking for some predefined values like vendor etc.?
2008@voice4all.com

J. from www.bzoox.nl / www.bbayt.com