Dynamics AX - Get table buffer (Common) from tableId

maqk® by MolviDSLR Khan | 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


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


mani said...

very usefull notes and we need more information on this topic please post updated data . thanks for your post.
Dot net online tutorials

Post a Comment