Using Computed Columns in Microsoft Dynamics AX 2012
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.
My view has only CustTable as its DataSource. The AccountNum field will be displayed, and the Name field is my Computed Column.
Note that the properties on the Computed Columninclude a ViewMethod property. The getName static method is as follows…
static server str getName()
(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,
expression = ‘select name from dirpartytable where recid = ‘ +
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!
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