3

How can you use the syscomputedcolumn class to retrieve a table or field name for an entity? this is fairly easy using virtual field entity postload method something like

public class SysDatabaseLogHeaderEntity extends common
{
    public void postLoad()
    {
        super();

        this.TableName = tableId2Name(this.table);
    }
}

but there's a rumour that virtual fields won't be supported in upcoming synapse link for D 365 FnO so want to know how to do this with computed columns...

https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/data-entity-computed-columns-virtual-fields

jhowe
  • 10,198
  • 19
  • 48
  • 66

1 Answers1

1

SysComputedColumn is used to help create computed columns in views.

Supposing for some reason you want a column in which every row contains the string value "CustTable", you'd create create a method (AX 2012 syntax):

public static server string TableNameColumn()
{
    return SysComputedColumn::returnLiteral(tableStr(CustTable));
}

and then you'd add a computed column to the view as outlined here: https://learn.microsoft.com/en-us/dynamicsax-2012/developer/walkthrough-add-a-computed-column-to-a-view

Note: hopefully this is a toy example, there is no reason to ever actually do this particular column. Or really any fully static columns.

View computed columns are essentially based on static server methods which return the SQL definition for the computed column, and then the SysComputedColumn class has a bunch of helper methods to let you build those SQL string statements without using specific implementation knowledge of the backend database such as column names.

A complete description is beyond the scope of this comment, but the big one you'll use is SysComputedColumn::returnField(view,datasource,field) which gets the specified field from the specified datasource in the specified view. You want to use intrinsic functions for these parameters to keep your cross references valid (https://learn.microsoft.com/en-us/dynamicsax-2012/developer/intrinsic-functions).

There will be a lot you can't do though. These are sql only so they cannot send individual rows into X++ business logic. You need to reconstruct said business logic in SQL which can't always be done easily.

I like to keep those methods public so I can info them out and test them in SQL directly. I'm guessing you can't access the sql in d365, but looking at the string returned from your method can still help in troubleshooting.

Luke Kubat
  • 371
  • 1
  • 4
  • Hi, thanks this is useful however I already know all about syscomputedcolumns, how do I retrieve the tablename for sysdatabaselog.table as shown in my example using syscomputedcolumns? i'm trying to convert code from postload method to syscomputedcolumns method... in postload method i can use but don't have same equivalent using syscomputedcolumns... – jhowe Sep 12 '22 at 12:52
  • This would be an example of sending data from a row (this.table) into X++ business logic (tableid2name). You can't do this exactly. You could create a view that joins SysDatabaseLog to SysModelElement on the AxId field and get the name as a regular column. You need to filter to the element type of "Table" which I beleive is 77. Then you can replace the SysDatabaseLog table with this view in your data entity. You don't even need computed columns. – Luke Kubat Sep 13 '22 at 13:30