+ Start a Discussion
sf@sfsf@sf 

50001 To Many Rows Exception

Hi, I wanted to Query as 

 

Consider there are more than  50000 records in lead in production.

 

[SELECT Count(id) from Lead Limit 1]

 

Idealy it Returns only one record.

as i understand as in docs its mentioned it will throw an Too many rows Exception. 

 

SO how can i avoid this Exception. Or Batch Apex is the only solution for this. 

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

If you execute a method that is marked as readonly, you can query more records.  In Winter '13 they will allow the readonly markup to query unlimited records.

All Answers

sfdcfoxsfdcfox

Batch Apex would be the answer, yes. Even though it only returns one ROW, it is querying all rows in the database, so you end up querying all RECORDS.

sf@sfsf@sf

Hi Thanks for the immediate Reply.

 

Can  i know how can i achive this.

 

Lead has 60000 And scope of batch is  2000 so every time batch is executed this size of records i retrive is 2000 in execute method. Now how do i Add the First batch and the succeeding batches.

 

 

sfdcfoxsfdcfox

See the discussion in the Apex Code Developer's Guide in regards to Database.Stateful, which allows you to store information between each call to execute() of your class.

sf@sfsf@sf

Thanks sfdc

 

Damien_Damien_

If you execute a method that is marked as readonly, you can query more records.  In Winter '13 they will allow the readonly markup to query unlimited records.

This was selected as the best answer
SFDC-SDSFDC-SD

Try using logic by using OFFSET. This is available as a Developer Preview.

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_offset.htm

you can

sfdcfoxsfdcfox

OFFSET is no longer in Preview, by the way.

 

Edit: That's odd, the docs say it is, but I can execute it in all my orgs, even my production org...

sf@sfsf@sf

Thanks Damien.