-->

Custom code snippets in AX 2012

maqk® by Unknown | 11:04 PM

Blogging is all about sharing, specially when you are a technical blogger. And sharing is caring. That said, my last blog was related to the class DictTable that provides metadata about an AOT Table. This class also has a method that returns a table buffer (common) on providing a valid table id.

There is a series of Dict classes including DictIndex and DictField. You can do wonders with these classes as they are there to do wonders. One of the wonders is just about to be shared and with no more wait, here it is:

Code Snippets in AX 2012


The post is great, just they did not mentioned that you may need to restart Dynamics AX AOS which i had to to get all gears running and all code working :)

The whole story is about two classes
  • xppSource (holds the code that on runtime, generates the text you want to appear as a template)
  • EditorScripts (calls the xppSource method when an option selected from Script menu or TAB hit after short code typed)

There are other good posts on similar topics as well like
&

Dynamics AX - Get table buffer (Common) from tableId

maqk® by Unknown | 1:33 AM

The first question that flashes after reading the post title is Why ever ? The simple answer is, i was stuck into one such scenario which led me to this effort.

actually this can be very handy, if you want to declare / instantiate your table buffers in one centralized place, say a static method of a class. This will save code lines and redundancy

Scenario

To be more comprehensive, let me explain the scenario I faced for all of this
I was writing a method that would return a valid existing recId of any table that i send to the method as a param

Now a rough code sketch for this will be something as follows;
static RecId methodName(Common _common)
{
     
    select minOf(RecId) from _common;
    minVal = _common.RecId;
    select maxOf(recId) from _common;
    maxVal = _common.RecId;
   
    recordId = xGlobal::randomPositiveInt32() mod (maxVal - minVal) + minVal;
  
    return recId;
}

The method is effective, fullfilling my requirement.

However at the time of consumption, it needs a specific initialized table buffer. Heres a valid consumption call to this method.

RecId recId;
MyTable myTable; //This is the specific 'MyTable' table buffer
recId = ClassName::methodName(myTable);

At this point of time, its obvious that for any table other than the 'MyTable', i have to declare it and send it to my 'random' method. Whereas intrinsic methods like tableNum() for instance require types or classes, not initialized objects....

There should be a way to send tableId at the conumption point so that a table buffer is initialized at the target method only, thus giving me the ease and rid from declaring each specific table every time my random method is called.

In short we can say we want a method that returns a Common object for the TableId being sent to it, So what we do....... well we search harder untill we find this

Quoting from the excellent post there,

public Common findRecord(TableId _tableId, RecId _recId, Boolean _forUpdate = false)
{
    Common      common;
    DictTable   dictTable;
    ;
    dictTable = new DictTable(_tableId);
    common = dictTable.makeRecord();
 
    common.selectForUpdate(_forUpdate);
 
    select common
    where common.RecId == _recId;
 
    return common;
}


Using the DictTable class (it seems to be, although i cant find it in the AOT :) ), and the assigning the return from makeRecord instance method to common object, you just have got your specific table buffer by just providing its tableId saving you from declaring each specific table at time of method consumption,

just send tableNum(TableName) to the method and you are done. Coments are welcome :)
top