Virtual field not taking values

Hi, I have created a virtual field that has a computed value. However, the field value is not coming. The primary table is salesline and the method is:

public static str ItemName()
{
    DictDataEntity de   = new DictDataEntity(tableNum(CustomSalesOrderLineV2Entity));

    int64 salesLineRecId  = str2Int64(de.computedColumnString(tableStr(SalesLine),
                                                            fieldStr(SalesLine, RecId),
                                                            FieldNameGenerationMode::WhereClause));
    SalesLine salesLine;

    select firstonly salesLine where salesLine.RecId == salesLineRecId;

    return salesLine.itemName();
}

On changing the code I passed the salesLineRecId as string and I found its value is coming as 0. Though the recid field is present in data entity as field recidcopy1. Can anyone please help me understand where I might be going wrong.

You misunderstand the purpose of computed columns. The method should return T-SQL code for the computed column. It’s then called once, on database synchronization, and the T-SQL code is injected to the view definition in database. Your code does nothing like that.

You must decide - either you want to create a computed column and then you need completely different code. Or you want to use X++ code such as salesLine.itemName(), but then you can’t use a computed column. Use a virtual field instead.