Using Computed Columns in Microsoft Dynamics AX 2012

By Laura Lake | April 8, 2015

One of the complaints I hear most from customers is the inability to filter & sort on display methods. We have the ability in AX 2012 to use Computed Columns to add computed values to a view, and the interesting part is that these columns do support sorting and filtering. There are a few very good blogs online already showing examples of using computed columns with Sum() and other aggregate functions, but the intention here is to explore the possibility of having a computed column that is in itself a Select statement.

This example assumes the reader has knowledge of Views in AX. There is also a walkthrough on MSDN showing how to add a Computed Column to a View.

Walkthrough: Add a Computed Column to a View [AX 2012]

My view has only CustTable as its DataSource. The AccountNum field will be displayed, and the Name field is my Computed Column.

Computed Column Example

Note that the properties on the Computed Columninclude a ViewMethod property. The getName static method is as follows…

static server str getName()
{
return SysComputedColumn::getDirPartyNameFromParty(tableStr(CustView),identifierStr(CustTable),fieldStr(CustTable,Party));
}

(Note: The hard-coding in this example is intentional. I believe it helps to make the concept easier to understand for those who are new to it.)
I have created a new method in the SysComputedColumns class to return the Select statement which will be executed on SQL Server.

public static server str getDirPartyNameFromParty(TableName _viewName,
str _DataSourceParty,
str _DataSourcePartyField)
{
str expression;
expression = 'select name from dirpartytable where recid = ' +
SysComputedColumn::returnField(_viewName,
_DataSourceParty,
_DataSourcePartyField);

return expression;
}

SysComputedColumns has many built-in methods we can use as well, like the ReturnField method which here returns the value from the Party field in our CustView.

The resulting view looks like this, and notice I have clicked on the Name column to sort in Name order!

Table Browser
Finally, I am not advocating the use of computed columns to replace display methods under normal circumstances. However, sometimes our customers have business needs that require us to use whatever tools we have at our disposal. The possibilities here are intriguing but also dangerous. Think about how your views might be impacted by upgrades for one thing. With great power comes great responsibility!

For additional tips check out my post: More on Using Views in Dynamics AX 2012

Related Posts


Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!