SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex SOQL For Loop Simplified

So why do we need an Apex SOQL For loop? What is so special about it?

Apex SOQL For Loop comes in handy when you are dealing with a very large set of records. Your SOQL query sometimes may return so many records that the governor limit for heap size is exceeded and an error can occur.

Look at the below block of code :

List<Contact> conObject = [SELECT Id, Email FROM Contact WHERE Name Like '%blue%'];

If the SOQL query returns a very large set of records. It will be consuming a good amount of heap memory and as the record size increases it will be increasing heap memory consumption. Thus, it could lead to a run time exception.

As a remedy SOQL For Loop comes into the picture. Instead of pulling all the records in one go, it will process the records in batches or smaller chunks avoiding excess heap memory consumption.

Below block shows the SOQL For Loop in action :

List<Contact> contactList = new List<Contact>();

for(Contact con : [SELECT Id, Email FROM Contact WHERE Name Like '%blue%']){
    contactList.add(con);
}

What we did over here is instead of initializing contactList directly by SOQL at the top block. We are using SOQL for loop and populating contactList inside the for loop.

When you are dealing with DML Statements inside SOQL For Loop you should be a little careful to avoid breaching DML statement governor limits.

Example – Below blocks shows a not-so-good approach for using a DML statement inside a SOQL For Loop. This is applicable to any loop in Salesforce.

List<Contact> contactList = new List<Contact>();

for(Contact con : [SELECT Id, Email FROM Contact WHERE Name Like '%blue%']){
    con.Email = 'salesforceblue@gmail.com';
    // Not so good approach to have DML statement inside a for loop
    update con;     
}

So what is wrong with the above approach. Technically it will update the contact records and there seems to be no harm. However, when the records size increases let’s say more than 150 then you will see the Salesforce Governor Limits at your doorstep saying hey you can’t do a record update in this way!

This happens because at most 150 DML statements can be made in a single transaction.

Below is the recommended approach to do a DML statement inside a SOQL For Loop or any loop in Salesforce.

List<Contact> contactList = new List<Contact>();

for(Contact con : [SELECT Id, Email FROM Contact WHERE Name Like '%blue%']){
    con.Email = 'salesforceblue@gmail.com';
    // A good approach is to add the to be updated record in a list and later update the list outside the loop
    contactList.add(con);     
}

update contactList;

As you can see we have created a list contactList and added the updated contact records in this list inside SOQL For Loop. Later we are updating the contactList outside the loop. Salesforce will pat on your back if you always go with this approach for doing a DML outside the loop 🙂

If you wanted to know about the DML Statements & Governor Limits then you can take a quick peek at Apex DML Statements, and Salesforce Governor Limits.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


One thought on “Apex SOQL For Loop Simplified

Leave a Reply

Your email address will not be published. Required fields are marked *