The loop in Salesforce Apex code plays a vital role. This loop is specifically a block of code that continues until the condition is met. It is imperative here for the loop to meet the specified condition because it allows us to perform a task again and again. The loop is very often proven to be the greatest help to iterate and search the various records in the list. Here, the usage of nested loops can result in resource quick consumption and deliberately throw various kinds of errors during application execution. This is a huge necessity to optimise the loop in the Apex code.
Let us have a look into the very commonly occurring loop errors in this context, along with resolution opportunities:
Foreach Loop: It is very easy to write code for the loop iteration, but it has its performance issues.
You can avoid that issue with a foreach loop, which enables an internal iterator for the loop. You may check for each loop code below, which will create an object every time and then return it to the caller before retrieving the next value.
Avoid size () check for loop
The issue in the foreach loop is now easy to eradicate. The idea is to make the task of iterating the items in the loop simple.
If you investigate the above code, there is an issue, though it looks better for most scenarios. If you look into it clearly, you can see the usage of the size() method in the code. It is an indication of calculation while iterating, which can often impact the performance of the application. It is going to be a very time-consuming task when the record number is large for the completion of loop iterations. This issue can be handled very well in the code by using the variable in the loop, which will calculate the list of records and store the same in the variables. For this purpose, the improvised code by using a variable is as below:
This kind of code can serve well in all scenarios without causing any trouble for the performance of the application. Here, it is quite imperative to convey that the above loops can work well when not using any type of DB operation, like the creation of runtime collections or the operation on that calculation.
Accessing child loop collection
We very often get the response “Query Exception” when accessing child records in the loop. This kind of exception will appear as a response while trying to access larger sets of child records, like 200 or more.
The above loop can receive an exception message while trying to retrieve child contacts from an account that holds more than 200 records. This can be avoided quite easily in this context by improvising your loop code as below:
Heap size issues in the loop
Heap size issues are often seen during fetching records. This will happen when your loop queries for records to fetch all records where the object holds more than 20,000 records.
The above is a very common code example in this context to fetch records from the object. This code is perfectly good at fetching the records within the object, while records are few. The issue will occur when the volume of records is high, which is more than 20,000. In such situations, we often receive the response, “Apex heap size is too large”. Again, it is quite imperative here to optimise your loop in the code.
The above code can be a perfect match for this purpose. The way code above is enabled with the updating of records in the batches with 200 in for the loop. This approach will use DML for 200 records in the batch to avoid heap size issues.
Measuring heap size
If we want to measure the heap size, then “Limits” class usage in the apex code execution can be a great help.
- Limits.getHeapSize()—It will return the approximate size of memory that was used for the heap.
- Limits.getLimitHeapSize()—It will return the total volume of the memory available for the heap.
Knowing the heap size exceeds the below method in code can be a good choice, and based on the results, writing alternate code for handling the list is the best approach. The loop-throwing errors can be mitigated wisely by handling the loop efficiently in the code, which will enable optimised performance for applications.
The above-listed loop issues are very commonly seen, and CloudOdyssey is deploying the above-mentioned counteracts for optimising the loop in the apex code.