salesforce – What is the difference between soql for loop vs soql list
As per the documentation it mentions ) (soql for loop) retrieves all sObjects using a call to query and queryMore whereas (list for loop) retrieves a number of objects records. It is advisable to use (soql for loop) over (list for loop) to avoid heap size limit error.
Total Heap Size Limit : 6 M.B Synchronous and 12 M.B Asynchronous.
In below case, let say each record is taking 2 K.B so 50,000 will take 50,000*2=100000 K.B (100 M.B approx in conList) which will cause heap size limit error as the allowed limit is 6 M.B for synchronous.
list conList=new list();
conList=[Select id,phone from contact];
To avoid this we should use “SOQL for loop” as con variable highlighted below will have 1 record at a time i.e 2k.B of data at a time thus preventing heap size limit error.
for (List con: [SELECT id, name FROM contact]){
}
Question – What does it mean that SOQL for loop” as con variable highlighted below will have 1 record at a time i.e 2k.B of data at a time.
Read more here: Source link