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");

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));

// Set the delimiters

//Checking status of last operation
while (infile.status() == IO_status::Ok)
// Setting container according to datatypes
[itemId, itemName] = infile.read();
if (itemId)
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';

// 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;

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

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


isay said...

thanks men..you save my time :)

Anonymous said...


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?


Anonymous said...

answer already found. sorry for interrupting you.

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

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