如何检查是否可以使用下一个运算符?

How to check that next operator can be used?

任何 Axapta 版本的问题:

  1. 如何检查 'next' 运算符是否可以与记录一起使用?
  2. 如何在不可接受的 'next' 运算符上抑制调试器?

谢谢。


复制代码:

static void Job13(Args _args)
{
    CustTrans   ct1;
    CustTrans   ct2;

    // let's assume that method 'one' search a record
    select ct1; // where ct1.AccountNum == 'someAccount'
    ct2.data(ct1.data());

    // contract postcondition
    Debug::assert(ct1.RecId != 0);
    Debug::assert(ct2.RecId == ct1.RecId);

    //////////////////////////////////

    // let's assume that method 'two' accepts a custTrans record as parameter
    Debug::assert(ct2.RecId != 0);
    try
    {
        // Questions:
        // 1. How to check that 'next' can be used?
        // 2. How to suppress a debugger?
        next ct2;
    }
    catch
    {
        Warning('catch it!');
    }
}

+在 ax2009 中运行作业后创建的几个屏幕截图。

问题出在您的 ct2.data(ct1.data()); 并且是错误消息的 (d) 部分。看来 AX 无法处理这种情况。我同意@FH-Inway 的评论,您应该使用 while select ct1 {} 而不是 next.

下面演示的更清楚:

static void Job5(Args _args)
{
    SalesTable      salesTable;
    SalesTable      salesTable2;

    select salesTable where salesTable.SalesId == 'SO-001351';

    while (salesTable)
    {
        info(salesTable.SalesId);

        next salesTable;
    }

    info("Above has no issue");

    select salesTable where salesTable.SalesId == 'SO-001351';
    salesTable2.data(salesTable);

    while (salesTable2)
    {
        info(salesTable2.SalesId);

        next salesTable2;
    }

    info("Above fails");
}

根据MSDN article

The select statement only fetches one record, or field. To fetch additional records, you can use the next statement. The next statement fetches the next record in the table. If you use next without a preceding select command, an error occurs. Do not use next with the firstOnly find option. If you need to traverse a number of records, it is more appropriate to use a while select statement.

您已将 next 命令与 ct2 一起使用,而没有在 select 命令之前(您与 ct1 一起使用)。

更新:使用if (ct1.found()) next ct1;可以帮助您避免意外错误。