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
MohandaasMohandaas 

Using QueryMore in apex

Will QueryMore() help us retrieve more than 50000 records as against the 50000 SOQl rows apex governor limits.

 

Can anyone help me with a sample code to understand the usage. I could not find any useful reference also.

 

Appreciate your help. This is critical.

alexbalexb

It should help you. I can't recall the exact number that QueryMore will allow you to retrieve, but it does a lot. As I understand it, it runs in a loop, operating on smaller groups of the whole. For accessing the Salesforce database externally with a language like C#, you use the API and use the queryMore method:

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

 

I don't know if this is the same, but I think that doing a query in a loop like below will allow you to operate over a larger query result.

 

for ( Lead mylead : [SELECT Id, Name FROM Lead WHERE Name != null] ) {

if (mylead.Name == 'Alex')

mlead.Name = 'Alex2';

}

 

MohandaasMohandaas

Thanks for your response.

 

Rightly said it does a lot.I had used a SOQL for loop in my apex code after reading the below statement

 

"SOQL for loops differ from standard SOQL statements because of the method they use to retrieve sObjects. While the standard queries discussed in SOQL and SOSL Queries can retrieve either the count of a query or a number of object records, SOQL for loops retrieve all sObjects, using efficient chunking with calls to the query and queryMore

methods of the Web services API."

 

What I would like to confirm here is there a possibility to Query more than 50000 rows in apex. If not I can confidently speak to my client to go for a batch apex job.

alexbalexb

Well, if you are using the SOQL query in the for loop, as I showed above, then you should not be hitting any limit problems if you are writing this in Apex.

MohandaasMohandaas

I have hit the 50000 SOQL rows limit using the SOQL in a for loop.

forecast_is_cloudyforecast_is_cloudy

The SOQL for loop convention (i.e. the queryMore pattern) is limited to a max of 50,000 records. You can query >50,000 records using a new feature in Spring 11 - the @ReadOnly annotation. Caveats apply to the use of the @ReadOnly annotation however (e.g. no DML can be performed on the result set, has to be invoked from a Schedulable or Web Service interface etc.). You can find more details in the Apex Developers guide here -  http://www.salesforce.com/us/developer/docs/apexcode/index.htm.

 

If the @ReadOnly annotation doesn't work for your use case, the only way to query more than 50,000 records is via Batch Apex. Hope this helps...

MohandaasMohandaas

@ forecast_is_cloudy

 

This is a real good piece of information.Let me explore @ReadOnly annotation. Thanks.