Quick and Dirty Code to Create and Post Counting Journals in Dynamics AX
A couple of guys asked me for code to import inventory in to ax using Counting journals... so here goes..
I basically created a table, called StockLevelImport and added basic fields to it such as itemid, location, warehouse, size,colour and quantity..
Notice: other dimensions such as config, batch were not added..however you can easily do so if you like.
The following bit of code was use to import the stock file in to ax (basically import a csv file)
static int importFromFile()
{
CommaIo fileIn;
StockLevelImport stockLevelImport;
container fileInCon;
FileIOPermission perm;
int recordsInserted;
;
#stockLevelImport
// the macro holds the full file name
recordsInserted = 0;
perm = new FileIOPermission(#stockLevelImportFile,'r');
if (perm == null)
{
error("FileIoPermission error");
return 0;
}
// Grants permission to execute the CommaIo.new method.
// CommaIo.new runs under code access security.
perm.assert();
fileIn = new CommaIo(#stockLevelImportFile,'r');
delete_from stockLevelImport; // clear table before import
fileIn.inFieldDelimiter(',');
startLengthyOperation();
ttsbegin;
while(fileIn.status() == IO_Status::Ok)
{
fileInCon = connull();
fileInCon = fileIn.read();
stockLevelImport.ItemId = conpeek(fileInCon,1);
stockLevelImport.InventLocationId = conpeek(fileInCon,2);
stockLevelImport.WMSLocationId = conpeek(fileInCon,3);
stockLevelImport.InventQty = conpeek(fileInCon,4);
stockLevelImport.InventColorId = conpeek(fileInCon,6);
stockLevelImport.InventSizeId = conpeek(fileInCon,5);
if(fileIn.status() == IO_Status::Ok) // to prevent the last insert
stockLevelImport.insert();
recordsInserted++;
}
ttscommit;
endLengthyOperation();
CodeAccessPermission::revertAssert();
return recordsInserted;
}