Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

Can't seem to loop through datasource

Posted on by 200

Hi all,

I'm trying to create the following process:

2110.weightcheck.png

(1) Get packing lines and fill them in a temp table shown in a form

(2) Loop through records and calculate total weight when button is clicked

However if the button code below is executed it loops through the same amount of rows that is in the datasource but in my case the itemid is not updated. 

Any idea why?

void clicked()
{

    WHSTmpPackingLine loop = whstmppackingline_ds.getFirst();

    while (loop)
    {

        info(strFmt(WHSTmpPackingLine.ItemId));

        loop = WHSTmpPackingLine_ds.getNext();

    }

    super();
}

  • Berthil Profile Picture
    Berthil 200 on at
    RE: Can't seem to loop through datasource

    Thank you for your tip! I understand the logic now. I'm not much of a programmer and just conquering the concepts of X++.

  • Suggested answer
    vinitgoyal2005 Profile Picture
    vinitgoyal2005 6,330 on at
    RE: Can't seem to loop through datasource

    Hi Berthil,

    As mentioned by Gunjan, you need to use  info(strFmt(loop.ItemId));

    Also as a best practice do not use generic variables like 'loop' instead you can create variables like WHSTmpPackingLine whsTmpPackingLineloc = whstmppackingline_ds.getFirst(); and use whsTmpPackingLineloc in your logic. such code is more readable in the future by you or anyone else.

  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    RE: Can't seem to loop through datasource

    If it's a temporary table, you can simply use a select statement:

    while select whsTmpPackingLine
    {
        info(strFmt(whsTmpPackingLine.ItemId));
    }

    Or, if you want to take into account query ranges, you can use a query instead.

    Then you can also use sum() aggregation function to calculate the total.

  • Verified answer
    Gunjan Bhattachayya Profile Picture
    Gunjan Bhattachayya 35,421 on at
    RE: Can't seem to loop through datasource

    Try this instead for the info part  -

    info(strFmt(loop.ItemId));

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans