Monday, April 1, 2013

Cycle through container in AX

The below code shows how to cycle though a container in X++ for AX 2012.

Yes I know that the below can be done different. I'm doing things a certain way out of concept and writing it on a plane without a compiler. Also, you could use an array instead of a container since this is a single data type but I'm trying to show containers, not arrays.

I would suggest not using conIns for inserting elements at the end of the container as these inserts seem to get slower the larger the container being inserted into becomes. That's why I use the '+=' seen below.

    int         i;
    CustTrans   custTrans;
    container   custTransCont;    

    // Build the container with data
    while select custTrans
       where custTrans.AccountNum == 'CUS00001141'
       // The += is faster than the conIns() function
       //custTransCont = conIns(custTransCont, i, custTrans.RecId);
       custTransCont += custTrans.RecId;

    // Cycle (iterate) through the container
    for (i=1; i <= conlen(custTransCont); i++)
       info(strFmt("%1 - CustTrans RecId: %2", i, conpeek(custTransCont, i)));

No comments:

Post a Comment