function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

difference between storing SOQL results in list vs sObject

 So what is the different? To illustrate my point, here are both way returning a similar set.



Set<String> nameSet = new Set<String>(); List<Account> acct = [SELECT Name FROM Account]; for(Account a_insert: acct){ nameSet.add(a_insert.Name); }



Set<String> nameSet = new Set<String>(); for(Account acct : [SELECT Name FROM Account]){ nameSet.add(Name); }


Would I hit a limit earlier with #2 since I am storing the results in the sObject instead a list?




There is nothing different between the two.  The limits are the same and everything.  #2 is just a sort of short-cut.


Until Spring'10 release(latest release), Collections had a 1000 limit. So #1 would run into errors when your SOQL returned more than 1000 records. With the latest release this limit has been removed. However, storing the results of your SOQL in a collection and then iterating over it is inefficient. #2 is the preferred route for two reasons:


1. You can potentially iterate over upto 10000 records without hitting any limits

2. It's much more efficient from governor limit standpoint.