Wednesday, October 19, 2011

Find method for Dynamics AX temporary tables

how to create a find method for a temporary tables in Axapta, and the differences between normal and temporary tables in this case.

In general, each instance of a temporary table, and it's associated data, will only exist while the buffer variable used to access it is in scope.
You can point multiple buffer variables to the same instance of a temporary table by using either the .setTmpData() method or by directly assigning the buffers to each other, identically to normal tables. In this way, even if your original buffer variable goes out of scope, your data will be retained while one of the other referencing variables remains.
Be aware that static table methods -such as find()- will not work with temporary tables unless you pass through the buffer variable to the method.
For example, this method will not work on a temporary table, as the tempTable variable used is newly created and will always contain no records.


//This won't work on temporary table
public static TempTable find(AccountNum _accountNum, 
                             boolean    _forUpdate = false)
{
    TempTable tempTable;
    ;

    if(_accountNum)
    {
        tempTable.selectForUpdate(_forUpdate);

        select firstonly tempTable
            where tempTable.AccountNum == _accountNum;
    }

    return tempTable;
}

If you want to have a find() method on your temporary table, then you will need to modify it slightly to pass through a reference to our populated temporary table.

//Use this pattern instead
public static TempTable find(TempTable  _tempTable,
                             AccountNum _accountNum, 
                             boolean    _forUpdate = false)
{
    if(_accountNum)
    {
        _tempTable.selectForUpdate(_forUpdate);
        select firstonly _tempTable
            where _tempTable.AccountNum == _accountNum;
    }

    return _tempTable;
}

And we call the method this way:

TempTable     localTempTable;
AccountNum    localAccountNum;
;

...

localTempTable.setTmpData(populatedTempTable);
TempTable::find(localTempTable, localAccountNum);

if(localTempTable)
... 

Read more...

No comments:

Post a Comment