web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

I want to implement ComputedColumn for the case where there is the same value between two tables.

(2) ShareShare
ReportReport
Posted on by 28
I want to replace from a method to a view.
And also implement a comuputed column.
 
///////Method///////////////
public display AssociationTransferFlag transferFlag()
    {
        InventTransferTable                         inventTransferTable;     
        AssociationTransferFlag            transferFlag;
        select firstonly earth_dp_SalesId from inventTransferTable
                where inventTransferTable.earth_dp_SalesId == this.SalesId;
        
        if(inventTransferTable)
        {
            transferFlag = NoYes::Yes;
        }
        else
        {
            transferFlag = NoYes::No;
        }
  • Martin Dráb Profile Picture
    241,353 Most Valuable Professional on at
    All right, so what to do you need from us? Do you have any question?
     
    Which version of AX are you using?
  • Ko Yamazaki Profile Picture
    28 on at
    Hi Martin, 

    Thanks for reply.
    How can I implement here to add logic to see if the SalesTable has the same SalesID?
     
      private static server str transferFlag()
        {
            strYes = enum2Str(NoYes::Yes);
            str strNo = enum2Str(NoYes::No);
            str val1 = SysComputedColumn::returnField(viewstr(TransferFlagView),
                                                            identifierStr(InventTransferTable),
                                                            fieldStr(InventTransferTable, SalesId));
     
            str sqlQuery = strFmt("SELECT"+
                          "    CASE"+
                          "        WHEN ( %1 = '' ) OR ( %1 IS NULL ) THEN N'%2' " +
                          "        ELSE N'%3' " +
                          "    END", val1, strNo, strYes);
            return sqlQuery;
        }
  • Ko Yamazaki Profile Picture
    28 on at
    Product version is "10.0.38 (10.0.1777.115)".
    Platform version is "Update62 (7.0.7120.116)"
  • Martin Dráb Profile Picture
    241,353 Most Valuable Professional on at
    Moved from Dynamics AX forum.
     
    Using Yes and No strings is wrong for sure - the database stores enum values as numbers. You'll make your work easier if you test your SQL code before writing X++ generating it.
     
    I would think of something like EXISTS() to check if your query returned any rows. It'd give you 0 or 1, as you need. But I'd have to think about the design.
  • Suggested answer
    Layan Jwei Profile Picture
    8,353 Super User 2026 Season 2 on at
    Hi Ko,

    Maybe try sth like this (I'm not 100% sure of the syntax) but it should give u an idea

     
    return SysComputedColumn::if(
                            SysComputedColumn::equalExpression(
                                SysComputedColumn::comparisonField(
                                    tableStr(TransferFlagView),
                                    identifierStr( InventTransferTable),
                                    fieldStr( InventTransferTable, SalesId)
                                                                   ),
                                SysComputedColumn::comparisonField(
                                    tableStr(TransferFlagView),
                                    identifierStr(SalesTable),
                                    fieldStr(SalesTable, SalesId)
                                                                  ),
      ),                         SysComputedColumn::comparisonLiteral(NoYes::Yes),                         SysComputedColumn::comparisonLiteral(NoYes::No)                                     );
    I think maybe you should replace the last two lines with the following but​​​​​​​ you need to test it
    SysComputedColumn::returnLiteral(1)
    SysComputedColumn::returnLiteral(0)



    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Suggested answer
    Martin Dráb Profile Picture
    241,353 Most Valuable Professional on at
    The last answer doesn't take the subquery into account. The easiest way to get the requested result from a subquery will be something like this: ISNULL(SELECT 1 FROM ...), 1).
  • Layan Jwei Profile Picture
    8,353 Super User 2026 Season 2 on at
    I think Martin is correct,

    I believe what Martin is trying to say is to do it like this:
     
    public static server str TransferFlag()
        {
    
            return  strFmt('ISNULL((select top 1 1 from InventTransferTable as inventTransferTable WHERE inventTransferTable.PARTITION = %1 AND inventTransferTable.DATAAREAID = %2 AND inventTransferTable.SalesId = %3), %4)'
                , SysComputedColumn::comparisonField(tableStr(TransferFlagView), identifierStr(SalesTable), fieldStr(SalesTable, Partition))
                , SysComputedColumn::comparisonField(tableStr(TransferFlagView), identifierStr(SalesTable), fieldStr(SalesTable, DataAreaId))
                , SysComputedColumn::comparisonField(tableStr(TransferFlagView), identifierStr(SalesTable), fieldStr(SalesTable, SalesId))
                , 1
                 );
    
        }
    This code will give you sth similar to this query:
    SELECT        T1.Field1, T1.Field2, T1.SALESID, T1.DATAAREAID, T1.PARTITION, T1.RECID, T2.SALESID AS SalesTableSALESID, T2.DATAAREAID AS DATAAREAID#2, T2.PARTITION AS PARTITION#2, CAST(ISNULL
                                 ((SELECT        TOP (1) 1 AS Expr1
                                     FROM            dbo.InventTransferTable AS inventTransferTable
                                     WHERE        (PARTITION = T2.PARTITION) AND (DATAAREAID = T2.DATAAREAID) AND (SALESID = T2.SALESID)), 1) AS NVARCHAR(10)) AS TransferFlag
    FROM            dbo.InventTransferTable AS T1 JOIN
                             dbo.SALESTABLE AS T2 ON T1.SALESID = T2.SALESID AND T1.DATAAREAID = T2.DATAAREAID AND T1.PARTITION = T2.PARTITION

    while the first approach will give you sth similar to this query

    SELECT        T1.Field1, T1.Field2, T1.SALESID, T1.DATAAREAID, T1.PARTITION, T1.RECID, T2.SALESID AS SalesTableSALESID, T2.DATAAREAID AS DATAAREAID#2, T2.PARTITION AS PARTITION#2, 
                             CAST((CASE WHEN T1.SALESID = T2.SALESID THEN 1 ELSE 0 END) AS NVARCHAR(10)) AS TransferFlag
    FROM            dbo.InventTransferTable AS T1 JOIN
                             dbo.SALESTABLE AS T2 ON T1.SALESID = T2.SALESID AND T1.DATAAREAID = T2.DATAAREAID AND T1.PARTITION = T2.PARTITION


    Always write your SQL when trying to do computed columns to make sure your computed column is returning what you want. If this is not what you want then please share more details


    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Layan Jwei Profile Picture
    8,353 Super User 2026 Season 2 on at
    Hi Ko,
     
    Is ur issue fixed now? If yes then please verify the answers that helped.
  • Ko Yamazaki Profile Picture
    28 on at
    Hi Layan,
     
    Not yet, I'm checking.
  • Layan Jwei Profile Picture
    8,353 Super User 2026 Season 2 on at
    Hi Ko,
     
    If ur issue is solved, then please verify the answers that helped

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 539 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 431 Super User 2026 Season 2

#3
CU-1234529-002 Profile Picture

CU-1234529-002 184

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans