2

I have come across a strange syntax that I have never seen in x++ before, but it compiles and works (as far as I can tell). I was curious if anybody has seen or used this before and could explain: what is the purpose of the if statement within the context of a while select?

InventBatch inventBatch;
InventTrans inventTrans;
InventTransOrigin inventTransOrigin;
InventDim inventDim;

ttsBegin;

while select Qty, DatePhysical from inventTrans
    where inventTrans.StatusReceipt == StatusReceipt::Arrived
    join inventTransOrigin
    where inventTransOrigin.RecId == inventTrans.InventTransOrigin
        && inventTransOrigin.InventTransId == "SomeIdFromSomewhere"
        join inventDim
            where inventDim.inventDimId == inventTrans.inventDimId 
            && inventDim.inventBatchId 
if (inventTrans)
{
    inventBatch = InventBatch::find(inventDim.inventBatchId, inventTrans.ItemId, true);
    inventBatch.Field1 = inventTrans.Qty;
    inventBatch.Field2 = inventTrans.DatePhysical;
    inventBatch.update();
}

ttsCommit;
rjv
  • 1,058
  • 11
  • 29

1 Answers1

4

When you do a while select, generally you put {} to wrap your code, but you can also do the same thing as if statements, if you omit the {} and the immediately proceeding line gets executed for each loop.

if (true)
    info("Hello World");

if (true)
{
    info("Hello World");
}

while select SalesTable
    info(SalesTable.SalesId);

while select SalesTable
{
    info(SalesTable.SalesId);
}

Regarding the code you have typed above, it's idiotic. In AX, in older versions of the code if (common) would often only evaluate common.RecId != 0, but in later ones, I believe it will evaluate true if the buffer is returned with some data. In a while select however, it will always return true as the select is only returning records when it's true.

You could/should just delete literally only the if (inventTrans) line and leave the brackets and it will be readable/normal code.

Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71
  • 1
    My gut feeling when I encountered the code was "this seems idiotic", so I'm glad to see my gut wasn't too far off :) – rjv Jul 25 '19 at 17:27