问题
Questions for any Axapta version:
- How to check that 'next' operator can be used with record?
- How to suppress a debugger on unacceptable 'next' operator?
Thanks.
Code for reproduce:
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!');
}
}
+a couple of screenshots that created after job runs in ax2009.
回答1:
As per 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.
You have used next command with ct2 without preceding select command (which you used with ct1).
Update: Using if (ct1.found()) next ct1;
can help you avoid unexpected errors.
回答2:
The problem is your ct2.data(ct1.data());
and it is the (d)
part of your error message. It looks like AX just can't handle that scenario. I agree with @FH-Inway's comment, that you should probably use while select ct1 {}
instead of using next
.
The below demonstrates it more clearly:
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");
}
来源:https://stackoverflow.com/questions/50603129/how-to-check-that-next-operator-can-be-used