web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Problem with List iteration

(0) ShareShare
ReportReport
Posted on by

Hi I have following code, where I iterate a list and want to update all the record in that list. But its just the last record of the list that gets the update.

public server static PriceDiscAdmTable makeJournal(
    PriceDiscJournalName    _priceDiscJournalName,
    Name                    _journalDescription,
    container               _priceDiscList)
{
    PriceDiscAdmTrans   priceDiscAdmTrans;
    PriceDiscTable      priceDiscTable;
    RecordInsertList    priceDiscAdmTransInsertList    = new RecordInsertList(tableNum(PriceDiscAdmTrans), true);
    List                priceDiscTableList             = List::create(_priceDiscList);
    ListEnumerator      priceDiscTableListEnumerator   = priceDiscTableList.getEnumerator();
    PriceDiscAdmTable   priceDiscAdmTable;
    LineNum             lineNum;
    int i;

    ttsbegin;

    priceDiscAdmTable.JournalName = _priceDiscJournalName;
    priceDiscAdmTable.Name        = _journalDescription;
    priceDiscAdmTable.insert();

    while (priceDiscTableListEnumerator.moveNext())
    {
        lineNum++;
        priceDiscAdmTrans           = priceDiscTableListEnumerator.current();
        priceDiscAdmTrans.JournalNum= priceDiscAdmTable.JournalNum;
        priceDiscAdmTrans.LineNum   = lineNum;
        priceDiscAdmTransInsertList.add(priceDiscAdmTrans);

    }
    priceDiscAdmTransInsertList.insertDatabase();


    //NC - MAHF - Afslut perioden for en eksisterende pris - Begin
   while select forUpdate priceDiscTable
            // join priceDiscAdmTrans
            where
                priceDiscTable.ItemRelation == priceDiscAdmTrans.ItemRelation
                && priceDiscTable.Module == priceDiscAdmTrans.Module
                && priceDiscTable.AccountRelation == priceDiscAdmTrans.AccountRelation
                && priceDiscTable.AccountCode == priceDiscAdmTrans.AccountCode
                && priceDiscTable.ItemCode == priceDiscAdmTrans.ItemCode
                && priceDiscTable.Currency == priceDiscAdmTrans.Currency
                && priceDiscTable.QuantityAmountFrom == priceDiscAdmTrans.QuantityAmountFrom
                && priceDiscTable.InventDimId == priceDiscAdmTrans.InventDimId
                && priceDiscTable.NCServiceId == priceDiscAdmTrans.NCServiceId


        {
            // I run the update here. 
            priceDiscTable.reread();
            
             if(priceDiscTable.ToDate >= priceDiscAdmTrans.FromDate )
                {
                    priceDiscTable.ToDate = priceDiscAdmTrans.FromDate - 1;

                }
               priceDiscTable.update();
        }
//NC - MAHF - Afslut perioden for en eksisterende pris - End

    ttscommit;

    return priceDiscAdmTable;
}

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Could you please isolate the problem first? For example, can you confirm that your while select iterate right records? If not, it doesn't make sense to discuss updates. If it works, the code filling the data is irrelevant.

    By the way, what's the point in rereading what you've just selected by the while select?

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Martin

    thanks for your reply. You are right that the code is too long.

    I found the solution.

    When I move my While loop inside my While SELECT, it iterates the whole list. And its also makes very much sense. I was the one who didnt think that good in the beginning :)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans