Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Unanswered

QueryRun.next() only looping once, even though SysQueryRun::countLoops(queryRun) shows the total number of loops = 3

Posted on by

I am currently trying to loop through the data being pulled by the QueryRun object I made. The query generated by the filters/ranges I set does pull the correct rows when I translated the query to SQL. I also used SysQueryRun::countLoops(queryRun) to see how many rows were in the queryRun obj, and it said 3 (which is how many I excepted and how many where returned by the SQL query). 

However, when I debug and enter the while(queryRun.next()) loop, it only loops once before queryRun.next() returns false. Therefore, only one row is processed and returned. Is there another way to grab the rows that are tied to the queryRun obj?

  • apndt3 Profile Picture
    apndt3 on at
    RE: QueryRun.next() only looping once, even though SysQueryRun::countLoops(queryRun) shows the total number of loops = 3

    Thank you so much for your responses, but I just got it working! Thanks for you time I really appreciate it

  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,083 on at
    RE: QueryRun.next() only looping once, even though SysQueryRun::countLoops(queryRun) shows the total number of loops = 3

    Hi apndt3,

    Could you, please, share you query object is created (query structure)?

  • apndt3 Profile Picture
    apndt3 on at
    RE: QueryRun.next() only looping once, even though SysQueryRun::countLoops(queryRun) shows the total number of loops = 3

    Unfortunately it doesn't. I had added the SysQueryRun call as an attempt to see whether the queryRun object was matching up to the SQL statement produced and actually holding more than a single line. The issue with the loop had been happening prior to adding the call

  • Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,083 on at
    RE: QueryRun.next() only looping once, even though SysQueryRun::countLoops(queryRun) shows the total number of loops = 3

    Hi apndt3,

    You mentioned SysQueryRun::countLoops method. Is class name SysQueryRun or SysQuery? If you remove this call, does while(queryRun.next()) loop work correctly?

  • apndt3 Profile Picture
    apndt3 on at
    RE: QueryRun.next() only looping once, even though SysQueryRun::countLoops(queryRun) shows the total number of loops = 3

    Thank you for your response!

    The original code I had was:

    while (queryRun.next())

    {

    table1 = queryRun.get(tableId1); //main salesTable

    table2 = queryRun.get(tableId2);

    table3 = queryRun.get(tableId3);

    salesLine = salesLine::findId(table3);

    ...

    }

    I did try what you suggested a few days ago, but get this error :

    Stack trace: Cannot call the NEXT command at this point. Either: (a) the buffer was initiated on the server, (b) SELECT was never called, (c) SELECT was called but no more rows exists, or (d) data was assigned using the data() method.

    I tried just using one table and calling the other queryRun.gets in the while loop, but <next table1> returned false by the second iteration.

  • Blue Wang Profile Picture
    Blue Wang on at
    RE: QueryRun.next() only looping once, even though SysQueryRun::countLoops(queryRun) shows the total number of loops = 3

    HI Apndt3,

    Is it easy to share your code?

    While(queryRun.Next())
    {
        common = queryRun.get(tableId);
    }

    You can try to transform the code, although they mean the same.

    if(queryRun.next())
    {
       common = queryRun.get(tableId);
    }
    
    while(common)
    {
       ...;
       next common;
    }

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans