function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
daffentrangerdaffentranger 

Dynamic QueryMore in Apex?

Does APEX allow the ability to use the Query More operation in a dynamic way?  

 

I am able to use the querymore functionality via a for loop with a select statement however if I try and replace my select statement with a dynamic database.query I can't get it to work.  For example  I need something like below to work.  Any ideas?

 

 

String s = 'SELECT ID FROM PRODUCT2'; for (Product2 p: Database.query(s)) { // do something }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

Strange, mine returns every record.

This was my sample code, I ran in execute anonymous:

 

 

String s = 'SELECT ID FROM PRODUCT2'; Integer i=0; for (Product2 p: Database.query(s)) { system.debug('\n\n'+p); i++; } system.debug('\n\nGOT '+i+' rows');

 

 

 

All Answers

JimRaeJimRae
What is the issue you are having when doing this?  I am able to do this without an problem.
daffentrangerdaffentranger
It will actually compile without any syntax issues however it returns no rows. 
JimRaeJimRae

Strange, mine returns every record.

This was my sample code, I ran in execute anonymous:

 

 

String s = 'SELECT ID FROM PRODUCT2'; Integer i=0; for (Product2 p: Database.query(s)) { system.debug('\n\n'+p); i++; } system.debug('\n\nGOT '+i+' rows');

 

 

 

This was selected as the best answer
daffentrangerdaffentranger
Using your example it looks like it does work.  After review I had a more complex query that appeared to be errantly not returning any rows.  Thanks