Skip to main content

Avoid hitting governor limits

DML operation
Using DML statement inside the for loop is not a good practise.Total number of DML operation can be performed in a Apex class is 150. The following example doesn't bulkify DML operation, it will throw an Apex governor limit exception.

In this example for loop iterates over LineItem contained in the liList list variable, for each iteration it will update Line item with new description. It will work up to 150 recrds, for the 151st records this trigger throws an run time exception for exceeding governor limit for DML operation.

So how can we over come this issue? one straight answer is avoid DML operation inside the for loop
Below example is recommended alternative code which will work in Bulk operation

More efficient SOQL Queries
Similar to DML operation placing the SOQL query inside the loop is not a good practise. Total number of SOQL query issued in the apex class is 100. The following example
shows the inefficient querieng of child item.

The SOQL query performed inside the loop retrieves the Line item for each invoice statement.
When updating more than 100 Invoice statement records, it will throw the run time exception for exceeding governor limit for SOQL queries.

Below is the recommended alternative code for SOQL query.This example bypasses the problem of having the SOQL query called for each item. It has a modified SOQL query that
retrieves all invoice statements that are part of and also gets their line items through the nested query. In this way, only one SOQL query is performed and we’re still within our limits

SOQL For Loop
Use SOQL for loop to operate on records in batches of 200, it helps to avoid heap size limit of 6 MB

Query without For Loop
The following example of query will return all the Account records in your organization and store it in the list, suppose the returned Invoice_Statement are larger size
and a large number of them was returned , the heap size limit might be hit

Recommended alternative code for querying within a for loop
To prevent the heap limit error use SOQL for loop, which iterates over the returned results in batches of 200 records. This will reduces the size of the inv list
variable which will now hold the 200 items, instead of all the items in the query result.

So array/list variable size limit is dependent upon the heap size.

  • Avoid using SOQL and DML query inside loop.
  • Use SOQL for loop to avoid heap size limit.
For complete list of apex governor limit, see this Link


  1. I've run into this issue from legacy code. Thanks for the post.


Post a Comment

Popular posts from this blog

Avoid Duplicate Records Using Flow

In this blog post lets see how to avoid duplicate records in Salesforce using the powerful feature – Flow , As the word says, flow easily flows to fulfill the requirements with ease. ☺  Many think of appexchange solutions and source code to avoid the duplicate record in salesforce, But the method I'm going to explain here is completely different and very simple. It helps you to reduce your code and can easily rebuild for other objects with different field logic with simple clicks. Lets jump into the solution, we will use flow to check duplicate record logic and use apex trigger to initiate the Flow whenever a record is created or updated. Visual Work Flow : [Click] First we will create the Flow to check the duplicate records based on the name and store the result in the variable. In this example, we will use the Account object for the dupe check. Step 1  : Create a new visual flow with the Name ‘ DuplicateAccountCheck ' Step 2  : Click and drag the RecordLookup into t

Plan your Dreamforce with “Dreamforce Trail”

Salesforce has many good resources for anybody looking to begin Salesforce as beginner, Intermediate or Advanced. Trailhead is also one among the good resource to learn salesforce and my favorite too. I have also written blog post about Trailhead & my favorite module   This  blog post covers an interesting and helpful Trailhead module. Yes Its all about ' Dreamforce Trail ', if you are a Dreamforce Attendee, then I strongly recommend you to take a look into this Trailhead module. No Admin skill or No Developer skill is required for this module.Start winning Badges and is gamified so it motivates you to reach more badges and finally you end up gaining knowledge on Salesforce. Why do we have Dreamforce Trail module in Trailhead?  To help people who is coming to dreamforce for the first time and to plan well for the events. Dreamforce is a tech feast for anybody. Dreamforce is salesforce festival. If you don’t plan well, you will miss something highly helpful . This Dre

Export records from List view – ListView API

As part of the Winter’15 release salesforce introduced the List View API features which helps us to get details and records of list views for an object. I was started exploring more about the List view API and trying to build an use case using that. So I searched about List View in the IdeaExchange site then found that many users are looking for a solution to export records to excel directly from the list view. I built a solution for that Idea, which helps user to download all the records directly from the List view for both standard and custom object. You don’t have to make any change in the code to use this functionality for different objects. All you need is to create a custom button for that object as mentioned below. Lets take an example for Account object 1. Go to Setup –> Accounts –> Buttons, Links and Actions 2. Click ‘New Button or Link' 3. Enter  Label, Name and select display type as ‘List Button’ 4. Content source as “Onclick Javascript” and paste the