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

Notifications

Announcements

No record found.

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
    237,965 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans