Thursday, January 3, 2013

Global findByRecId() function

AX 2012 relies heavily on foreign key relations for its table relations. A consequence of this change is that now developers will be accessing tables using recIds rather than using any other primary key, which was the norm before. So far so good. So what's the problem, you may ask.
Now this will also mean that developers will need to write lot more findByRecId() methods on tables. That is redundancy. We can refactor this method as a generic template and write it in Global class.

One example I found in an excellent blog post.

public Common findByRecId(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;
Now when you need to access a record buffer in a table MyTable using record id, instead of writing it as MyTable::findByRecId(_recId), you can use findByRecId(tableNum(MyTable), _recId)

"Execute business operations in CIL" or not?

In AX 2012, steps are taken by the development team at Microsoft to align AX IDE with .NET. That's why you regenerate the CIL after X++ code changes.

CIL obviously has its benefits like faster execution in cases of heavy logic processing.

But sometimes while testing or debugging, developers would prefer NOT to avail this option. Debugging can be cumbersome in AX 2012 as you will need to configure Visual Studio for code running in CIL. The solution is to uncheck a checkbox in Options at
Tools > Options > Development > General > Execute business operations in CIL

Now even the code which was running in CIL will now run in X++ interpreted mode, the old way.

Word of caution, this option should only be used by developers while debugging. It should ideally be checked again after work is finished.