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
devendra dhakadevendra dhaka 

SOQL query crossing limits

 

 

 

 List<Id> ltFromLeavePolicy = new List<Id>();
                  for(Leave_Policy__c lp :additionalLeavePolicy)
                           ltFromLeavePolicy.add(lp.LeaveType__c);

 

List<Leave_Balance__c> lbList = [SELECT Leave_Credit__c FROM Leave_Balance__c WHERE LeaveType__c in : ltFromLeavePolicy];

 

 

Above SOQL query is fetching a large amout of records which is bursting limits. Can we fetch records in batches ??

Navatar_DbSupNavatar_DbSup

Hi,

 

Apex Code is the Force.com programming language used to write custom, robust business logic. As with any programming language, there are key coding principles and best practices that will help you write efficient, scalable code.

 

Best Practice #1: Querying Large Data Sets

 

The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore.

 

 

Best Practice #2: Bulkify your Code

 

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. When a batch of records initiates Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch.

 

Best Practice #3: Avoid SOQL Queries inside FOR Loops

 

 A common mistake is that queries are placed inside a for loop. There is a governor limit that enforces a maximum number of SOQL queries. When queries are placed inside a for loop, a query is executed on each iteration and the governor limit is easily reached. Instead, move the SOQL query outside of the for loop and retrieve all the necessary data in a single query.

 

 Note : Executing queries or DML operations within an iteration adds risk that the governor limits will be exceeded.

 

Best Practice #4: Using Collections, Streamlining Queries, and Efficient For Loops

 

 It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits.

 

 Best Practice #5: Streamlining Multiple Triggers on the Same Object

 

 It is important to avoid redundancies and inefficiencies when deploying multiple triggers on the same object. If developed independently, it is possible to have redundant queries that query the same dataset or possibly have redundant for statements.

 

Note that it is very important to detail exactly how governor limits are applied when multiple triggers are deployed on the same object.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Noam.dganiNoam.dgani

what is a large amount? 50K? 500K?

 

you can use Batch Apex: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

to chunk up your process.

 

additionally, you can probably do something like:

 

for(Leave_Balance__c lb: [SELECT Leave_Credit__c FROM Leave_Balance__c WHERE LeaveType__c in : ltFromLeavePolicy])

{

//your logic

}

 

salesforce chunks the data for you - but if your talking about 50K records+ i would go with Batch Apex.

 


yvk431yvk431

Can you explain what you are trying to do here

 

--yvk