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
s9s9 

can we use soql in for loop

Hi,

 

Can we use soql in for loop?if yes how can we use? what is difference between standard soql and soqlforloop?is there any governer limits? with example

 

Thanks

S9

AmenaAmena
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 SOAP API. Developers should always use a SOQL for loop to process query results that return many records, to avoid the limit on heap size. Please refer : http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm
RockzRockz

Hi...

 

 

Avoid Soql Query in For loop:http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

 

Understand Governors and Limits:

 

Since Apex runs in a multitenant environment, the Apex run time engine strictly enforces a number of limits to ensure that runaway Apex does not monopolize shared resources. These limits, or governors, track and enforce the statistics outlined in the following table. If some Apex code ever exceeds a limit, the associated governor issues a run time exception that cannot be handled.
 
 Governor Limits in Salesforce :
 
  1. Total number of SOQL queries issued---100
  2. Total number of SOQL queries issued for Batch Apex and future methods--200
  3. Total number of records retrieved by SOQL queries--50,000
  4. Total number of records retrieved by Database.getQueryLocator--10,000
  5. Total number of SOSL queries issued--20
  6. Total number of records retrieved by a single SOSL query---200
  7. Total number of DML statements issued-----150
  8. Total number of records processed as a result of DML statements, Approval.process, or database.emptyRecycleBin--10,000
  9. Total number of executed code statements---200,000
  10. Total number of executed code statements for Batch Apex and future methods---1,000,000
  11. Total heap size---6 MB
  12. Total heap size for Batch Apex and future methods---12 MB
  13. Total stack depth for any Apex invocation that recursively fires triggers due to insert, update, or delete statements--16
  14. For loop list batch size--200
  15. Total number of callouts (HTTP requests or Web services calls) in a request--10
  16. Maximum timeout for all callouts (HTTP requests or Web services calls) in a request---120 seconds
  17. Default timeout of callouts (HTTP requests or Web services calls) in a request---10 seconds
  18. Total number of methods with the future annotation allowed per Apex invocation---10
  19. Maximum size of callout request or response (HTTP request or Web services call)----3 MB
  20. Total number of sendEmail methods allowed---10
  21. Total number of describes allowed---100
  22. Total number of classes that can be scheduled concurrently--25
  23. Total number of test classes that can be queued per a 24–hour period
  24. The greater of 500 or 10 multiplied by the number of test classes in the organization.

 

SOQL For Loops - Apex

 

Queries can be embedded in the special for syntax. This syntax can be used to loop through the sObjects returned by the query, one at a time, or in batches of 200 sObjects when using a list variable to hold the query results. Using a list variable to hold the query results in the SOQL for loop is a good way to query a large number of records since this helps avoid the heap limit, which is one of the governor execution limits. 

Here is a SOQL for loop example. In this example, each iteration of the for loop operates on a single sObject returned by the query. This is inefficient if you perform database operations inside the for loop because they execute once for each sObject and you’re more likely to reach certain governor limits.
for (Merchandise__c tmp : [SELECT Id FROM Merchandise__c]) {
   // Perform some actions on the single merchandise record. 
    
}
A more efficient way is to use a list variable to hold the batch of records returned by each iteration of the for loop. This allows for bulk processing of database operations. The following example uses a list variable in the for loop.

for (Merchandise__c[] tmp : [SELECT Id FROM Merchandise__c]) {
   // Perform some actions on the single merchandise record. 
    
}
 
Thanks,
Cool Sfdc