Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

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

(0) ShareShare
ReportReport
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,091 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,091 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;
    }

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans