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
JitendraJitendra 

Large query result error

Hi,

I am writting the Apex select statement and query returns the rows more than 800.
I read article on large query result and used for loop. but when i test the code it fails to compile and if i didnt test the code, query is executed succesfully on Live.
it is neccessary to test the code , otherwise my testing percentage will go below 75%.

My source code is:
Code:
Integer counter = 0;
 for(List<Account> opp : [Select a.Id  From Account a]){
  counter++;
 }

 
when i write below code the error is resolved:
Code:
Integer counter = 0;
 for(List<Account> opp : [Select a.Id  From Account a limit 300]){
  counter++;
 }

 My record may go above 2000 in future.

Can any body help me how to get rid off by this problem. :smileysad:

Thanks in advance!
Jitendra Zaa

mikefmikef
What are you trying to do?
What is the business use case?

I would like to know why you want an org wide count of Accounts every time you call this piece of code.

Also if you want a count you can use the count feature of SOQL.
[SELECT count() FROM Account ALL ROWS]
JitendraJitendra
mikef-
Thanks for immediate reply.

Actually i am building one VF page which will show the summary of account with additional calculated information which is not supported by standard reports.

Query result is more than 2000 records and if i bypass the test, it runs successfully on Live but whenever i tried to test the same function, it gives the error.

if i use Limit.getLimitedQuery(), then for test it is 500, and for live it is 10000.
In future if i like to fetch record more than 10000, this logic will not work. For loop of SOQL is not working for test cases, it doesn't fetch large result.

Can u please suggest any work around?

Thanks,
Jitendra
mikefmikef
10,000 query rows is an Apex limit.
So for the future you can't get around this.
Plus you have a 1,000 row limit if you process a query in a for loop.

You can process things in batch mode, and you would need to write this in your Apex code.

ie: Make the query then check if the rows returned are greater the 1,000. If so then do another query. And do this till you are done.
The issue is you have to have a new instance variable for every return set.

I am still not convinced you "need" this functionality. Sorry but this sounds like a Data Warehouse kind of process.

Bottom line is you have to filter some how before you query, and make sure less then 1,000 come back.
Plus what would users do with 1,000 rows on a screen?

See limits in the Apex docs.
JitendraJitendra
Thanks mikef,

Can you please send me the link of tutorial of batch mode?

My requirment is to generate the report with additional calculations of every account and opportunity, after that i am importing that info in excel sheet, thats why i need all the records,

Thanks,
Jitendra
mikefmikef
There is no tutorial for batch mode, you have to build it yourself in Apex code.

But as stated before it's not ideal cause you have no idea how many records you will have every time this runs.

This sounds more like a solution for an outside integration to a database and not Apex code.