Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

loop through table buffer object

Posted on by Microsoft Employee

hi,

one of my table is having find method which returns table buffer object.

In one of my class, i am calling this table's find method and getting that table buffer object.

Now i am doing , while select on that table buffer object. but to my surprise, it iterates through all records which are actually there in table. It should actually iterates through only those records which are there in my table buffer returned from find method.

what am i doing wrong here? kindly guide. Thanks.

example :

mytblbuffer =  mytable::find(somevalue);

while select mytblbuffer

{

it should iterate only for those records which are returned by find() method.

}

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: loop through table buffer object

    You can interate a selected table buffer with the next keyword:

    mytblbuffer =  mytable::find(somevalue);

    while(mytblbuffer)

    {

       //your code

       next mytblbuffer;

    }

    Granted your find method returns more than one value which it shouldn't.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: loop through table buffer object

    You've already got two suggestions: returning a temporary table buffer and returning a query object. Do you have any particular problem with these solutions?

    Also note that find() is not just a method as any other - developers expect that find() is an implementation of  static find Method Design Pattern.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: loop through table buffer object

    if not using find, what should be the standard way to return the multiple table rows?

    and how to loop through those records once you get those rows as part of table object returned?

  • Suggested answer
    Mohammad Raziq Ali Profile Picture
    Mohammad Raziq Ali 2,432 on at
    RE: loop through table buffer object

    Hi VDY,

    find() method returns only one record.

    You write while select query which retrieves the matched records.

    This while select query is new query to database. so, keep this in mind.

    If you run query 'n' times, it will be new 'n'queries  to database.

  • Ali Zaidi Profile Picture
    Ali Zaidi 4,657 on at
    RE: loop through table buffer object

    While select statement used to iterate on table buffer which populate on criteria of in where clause, if there is no where it return complete table.  In simple words about your code snippet, table buffer is populated or got reference form find method. and it is re initialized in while select  statement.

    your find statement will be like

    mytblbuffer =  mytable::find(somevalue);

    if (mytblBuffer !=null)

    {

    info(mytbleBuffer.Name);

    }

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: loop through table buffer object

    First of all, find() method should return exactly one record. If it returns more records, it shouldn't be called find(). Respect existing design patterns - others expect them to behave normally.

    Your expectation is wrong - your should realize that "while select" is a new select statement, e.g. a new query sent to database.

    In addition to Nigel's suggestion about temporary table, you could iterate the buffer with the next keyword. That does pretty much what you tried to develop, but it's not intuitive and it's used extremely rarely. I would prefer returning a query and running it when needed.

  • Suggested answer
    DaxNigel Profile Picture
    DaxNigel 2,572 on at
    RE: loop through table buffer object

    The standard find method is returning a record. So I am assuming your variable mytblbuffer is actually a table, which means when you run a select mytblbuffer you will infact be running a new select query on the table.

    The only way to do what you are wanting is to create a method on the table, like the find method, but that populates a temp table. You can then return the temp table and run the queries against that.

    Hope this helps.

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans