Computed column for stocked product

Hi everyone,

In my view I have the CustInvoiceTrans table, and there I have the StockedProduct field which is a NoYes enum. I have a new field that put 1 if the StockedProduct is active and 2 if it’s inactive.

This code shows only 1’s:

public static server str test()
{
TRV_VentasPorRenglon ventasporrenglon; //THIS IS MY VIEW

if(SysComputedColumn::equalExpression(SysComputedColumn::returnField(tableStr(TRV_VentasPorRenglon),identifierStr(CustInvoiceTrans_1),fieldStr(CustInvoiceTrans,StockedProduct)),‘1’))
return ‘1’;
else
return ‘2’;
}

This code shows only 2’s:

public static server str pruebaSQL()
{
TRV_VentasPorRenglon ventasporrenglon; //THIS IS MY VIEW

if(ventasporrenglon.StockedProduct == NoYes::Yes)
return ‘1’;
else
return ‘2’;
}

Am I doing something wrong?

Despite my effort to explain it in your previous thread, you still have no idea about how computed columns work.

The method must return T-SQL code computing the value - in your case, you always return a static value (either 1 or 2), which clearly isn’t useful.

Once more: computed column isn’t an method returning the given value, it’s a method returning T-SQL code that’s inserted to the view during synchronization. When you fetch data from the view, it executes the piece of T-SQL code; it doesn’t call the X++ method.

As I told you before, first define the SQL code that you want to use. I would expect something like:

case when CustInvoiceTrans.StockedProduct = 1 then 1 else 2

Then use AX helper functions, such as computedColumnString(), which help you generate the T-SQL with correct table names etc.

If your view doesn’t work as expected, open the view definition in SQL Server Management Studio and review the code that you’ve generated for your computed column.