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;
}
The code below is where meat of the operations are performed..
void countIt()
{
date transDate;
StockLevelImport stockLevelImport;
inventJournalName iJournalName;
InventJournalTable iJournalTable;
InventJournalTrans iJournalTrans;
real line;
Numberseq numberseq;
InventJournalCheckPost checkPost;
SysOperationProgress prog;
int progTot;// total count for progress bar
int ss;
InventTable inventTable;
InventTableModule inventTableModule;
inventdim idimm;
;
transDate = systemdateget();
#aviFiles
line = 1.00;
select count(RecId) from stockLevelImport;
progTot = stockLevelImport.RecId;
prog = SysOperationProgress::newGeneral(#aviStopWatch,"Adding Stock...", progTot);
numberseq = Numberseq::newGetNumFromCode(InventParameters::numRefInventJournalId().NumberSequence);
iJournalName = inventJournalName::find(InventParameters::find().CountJournalNameId);
if(iJournalName.RecId == 0)
throw error("NO counting journal Name foud");
ttsbegin;
iJournalTable.initFromInventJournalName(iJournalName);
iJournalTable.JournalId = numberseq.num();
iJournalTable.Reservation = ItemReservation::Automatic;
iJournalTable.SystemBlocked = NoYes::Yes;
iJournalTable.insert();
inventTableModule.clear();
inventTableModule = null;
stockLevelImport = null;
// while select inventtable
while select stockLevelImport where stockLevelImport.InventQty
join Price from inventTableModule where inventTableModule.ItemId == stockLevelImport.ItemId && inventTableModule.ModuleType == ModuleInventPurchSales::Invent
{
ss++;
iJournalTrans.clear();
iJournalTrans.initFromInventJournalTable(iJournalTable);
iJournalTrans.LineNum = line;
iJournalTrans.CostAmount = decround((stockLevelImport.InventQty * inventTableModule.Price),2);
iJournalTrans.CostPrice = inventTableModule.Price;
iJournalTrans.Counted = stockLevelImport.InventQty;
iJournalTrans.InventDimId = this.getDim();
if(iJournalTrans.InventDimId == "")
{
info("cannot create dimension for item " + iJournalTrans.ItemId);
continue;
}
iJournalTrans.JournalType = iJournalTable.JournalType;
iJournalTrans.PriceUnit = 1.0;
iJournalTrans.ProfitSet = CostProfitSet::Standard; // Standard
iJournalTrans.ProjSalesPrice = 0.0;
iJournalTrans.Qty = stockLevelImport.InventQty;
iJournalTrans.TransDate = transDate;
iJournalTrans.ItemId = stockLevelImport.ItemId;
iJournalTrans.initFromInventTable(InventTable::find(stockLevelImport.ItemId),false,false,false);
if(!iJournalTrans.Dimension)
{
info("Dimenstions not specified for item " + iJournalTrans.ItemId);
continue;
}
if(!iJournalTrans.validateWrite())
{
info("Could not validate write");
continue;
}
iJournalTrans.insert();
line++;
// progress bar
prog.setText("Item : " + stockLevelImport.ItemId);
prog.setCount(ss);
} // end while on Lines
iJournalTable.NumOfLines = line;
iJournalTable.update();
// Posting the Journal:
prog.setText("creating inventJournal...hold on");
prog.setCount(1);
if(BOX::yesNo("Do you want to post the Journal? " ,DialogButton::Yes) == DialogButton::Yes)
{
checkPost = InventJournalCheckPost::newJournalCheckPost(JournalCheckPostType::Post,iJournalTable);
checkpost.run();
}
iJournalTable.SystemBlocked = NoYes::No;
iJournalTable.update();
ttscommit;
prog.kill();
info("Lines posted = " + int2str(ss));
}
And finally a small method to find the correct inventDimId that is to be used on the journal Line.
InventDimId getDim()
{
;
if(!stockLevelImport.InventLocationId || ! stockLevelImport.WMSLocationId)
return "";
if(!WMSLocation::exist(stockLevelImport.WMSLocationId,stockLevelImport.InventLocationId))
return "";
idimm = null;
idimm.InventLocationId = stockLevelImport.InventLocationId;
idimm.wMSLocationId = stockLevelImport.WMSLocationId;
idimm.InventColorId = stockLevelImport.InventColorId;
idimm.InventSizeId = stockLevelImport.InventSizeId;
return inventdim::findOrCreate(idimm).InventDimId;
}
- Mohammed Rasheed
11:13 PM by Jawaan · 7
Dynamics AX: Counting Lines of Code – Mohammed Rasheed
It recently downed upon me that I do not have an account of the number of lines of code I have written on my vpc (my Dynamics AX Playground!!).... Unfortunately I could not find an easy way to get an accurate count of the lines of code I wrote.. Hence i wrote some more code :)
Its like writing code to count code!!
Hope this is of help to others out there.. asking themselves the same question.
class mrCountLinesOfCode
{
Map axTypeCount;
Map mrTypeCount;
MapEnumerator axEnumerator;
MapEnumerator mrEnumerator;
str fullPath;
int pos;
int total;
int linesOfCode;
}
void run()
{
TreeNodeiterator tempNodeIterator;
TreeNodeiterator treeNodeIterator;
TreeNodeiterator rootNodeIterator;
TreeNodeiterator childNodeiterator;
str type;
TreeNode treeNode;
TreeNode rootNode;
TreeNode childNode;
TreeNode tempNode;
;
#AOT
rootNode = TreeNode::rootNode();
rootNodeIterator = rootNode.AOTiterator();
rootNode = rootNodeIterator.next();
axTypeCount = new Map(types::String,types::Integer);
mrTypeCount = new Map(types::String,types::Integer);
while(rootNode)
{
type = rootNode.treeNodeName();
rootNodeIterator = rootNode.AOTiterator();
childNode = rootNodeIterator.next();
childNodeIterator = childNode.AOTiterator();
treeNode = childNodeIterator.next();
while(childNode)
{
tempNode = treeNode;
tempNodeiterator = tempNode.AOTiterator();
while(treeNode)
{
// loop till you get to a leaf node - as methods are leafs
while(treeNode.AOTchildNodeCount() > 0)
{
if(treeNode.AOTiterator().next().AOTchildNodeCount()> 0)
treeNode = tempNodeiterator.next();
if(!treeNode)
treeNode = tempNode;
tempNode = treeNode;
treeNode = treeNode.AOTiterator().next();
if(!treeNode)
treeNode = tempNode;
}
// so we found a Lefe. check if its a method
if(sysTreeNode::hasSource(treeNode))
{
tempNodeiterator = tempNode.AOTiterator();
while(treeNode)
{
treeNode = tempNodeiterator.next(); // iterate first.
this.setCount(treeNode,type);
}
treeNode = tempNodeiterator.next();
if(!treeNode)
{
treeNode = childNodeIterator.next();
if(!treeNode)
{
childNode = rootNodeIterator.next();
childNodeIterator = childNode.AOTiterator();
treeNode = childNodeIterator.next();
}
tempNode = treeNode;
tempNodeiterator = tempNode.AOTiterator();
}
}// end if Has Source
else
treeNode = tempNodeiterator.next(); // leaf is not a method.. hence move to the next node
}
//childNode = rootNodeIterator.next();
treeNode = childNodeIterator.next();
if(!treeNode)
{
childNode = rootNodeIterator.next();
while(childNode && !this.checkIfNodeMightHaveCode(childNode.sysNodeType()))
{
childNode = rootNodeIterator.next();
}
if(childNode)
{
childNodeIterator = childNode.AOTiterator();
treeNode = childNodeIterator.next();
}
}
}
childNode = rootNodeIterator.next();
rootNode = childNode;
}// end root while
this.printitOut();
}
void setCount(treeNode _treeNode,str _type)
{
int tempLines;
;
fullPath = sysTreeNode::getPath(_treeNode);
pos = 0;
pos = strScan(fullPath,'\mr',1,strlen(fullPath)); //i always prefix any new elements i create with MR
tempLines = this.getLinesOfCode(_treeNode.AOTgetSource());
if(tempLines > 4)
{
if(pos)
this.countMrLines(tempLines,_type);
else
this.countAXLines(tempLines,_type);
}
}
int getLinesOfCode(Source _source)
{
Source tmpSource;
Line line;
int ptr;
int numberOfLines;
;
if (_source)
{
tmpSource = _source;
while (tmpSource)
{
line++;
ptr = strfind(tmpSource, '\n', 1, maxint());
if (!ptr)
{
ptr = strlen(tmpSource);
}
numberOfLines++;
tmpSource = strdel(tmpSource, 1, ptr);
}
}
return numberOfLines;
}
int getLinesOfCode(Source _source)
{
Source tmpSource;
Line line;
int ptr;
int numberOfLines;
;
if (_source)
{
tmpSource = _source;
while (tmpSource)
{
line++;
ptr = strfind(tmpSource, '\n', 1, maxint());
if (!ptr)
{
ptr = strlen(tmpSource);
}
numberOfLines++;
tmpSource = strdel(tmpSource, 1, ptr);
}
}
return numberOfLines;
}
void countMrLines(int _lines,str _type)
{
;
linesOfCode = 0;
if(mrTypeCount.exists(_type))
linesOfCode = mrTypeCount.lookup(_type);
linesOfCode += _lines;
mrTypeCount.insert(_type,linesOfCode);
}
boolean checkIfNodeMightHaveCode(int _sysNodeType)
{
;
#TreeNodeSysNodeType
switch(_sysNodeType)
{
case #SysNodeTypeTableCollection :
case #SysNodeTypeSystemEnum :
case #NT_DBCOLLECTIONLIST :
case #NT_DBFIELDGROUPLIST :
case #NT_DBENUMLIST :
case #NT_DBREFERENCELIST :
case #NT_CONFIGURATIONKEYLIST :
case #NT_SECURITYKEYLIST :
case #NT_DBLICENSECODELIST :
case #NT_MENU :
case #NT_DBLICENSECODE :
case #NT_CONFIGURATIONKEY :
case #NT_SECURITYKEY :
case #NT_TYPE :
case #NT_REFERENCE :
case #NT_PERSPECTIVE :
case #NT_PERSPECTIVEFIELDLIST :
return false;
default:
return true;
}
// just in case
return true;
}
6:46 PM by Jawaan · 1