Development questions
What is a SOQL For Loop (for (List<Account> accList : [SELECT ...])) and how does it help avoid Heap Size limits?
A SOQL for loop iterates directly over a query result. Salesforce retrieves records in chunks of 200 instead of loading everything into one list, which keeps heap usage low.
apex
for (List<Account> chunk : [SELECT Id, Name FROM Account]) {
// process up to 200 records at a time
update chunk;
}
The single-record form for (Account a : [SELECT ...]) also streams in chunks. The 50,000 query row limit still applies, but heap size is not blown by a huge list.
soqlapexheapperformance
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?