Thursday, 19 January 2012

Loop through record from data source [AX 2012]


These are simple code snippets to loop through a record from a data source.

Using a while loop
    salesLine_ds    = _salesLine.dataSource();
    localSalesLine  = salesLine_ds.getFirst(true) as SalesLine;

    if (localSalesLine)
    {
        while (localSalesLine)
        {           
            //Do your thing
            localSalesLine = salesLine_ds.getNext() as SalesLine;
        }
    }

Using a for loop
   
    salesLine_ds = _salesLine.dataSource();
    for (localSalesLine = salesLine_ds.getFirst(true) ? salesLine_ds.getFirst(true) : salesLine_ds.cursor();
         localSalesLine;
         localSalesLine = salesLine_ds.getNext())
    {       
        //Do your thing
    }

Using MultiSelectionHelper class (blogged previously)
Using MultiSelectionHelper class (blogged previously)

2 comments:

Christopher Gerdes said...

In your while loop example, I believe that the initial if is unnecessary since you're checking the same condition before entering your while loop.

Munib Ahmed said...

True.
Too lazy to change it now. :)