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
MiddhaMiddha 

Getting query results in a Map with for loop

Hi All,

 

I am trying to query 10000 records and using a for loop o fetch them all. I know we can get these records in a list but can we get them in a map?

 

When i try :

 

 

for(Map<Id,Account> aMap : new Map<Id,Account>([Select Id,Name from Account])){}

 

 I am getting this error: Loop must iterate over a collection type: MAP:Id,SOBJECT:Account
 
Please advise. 

 

WesNolte__cWesNolte__c

Hey

 

You're going to hit a governor limit with 10000 records, I think it'll only let you work with a 1000 in a map. Anyway, you were almost there, you'll need to do it in two steps though,

 

Map<Id,Account> aMap = new Map<Id,Account>([Select Id,Name from Account LIMIT 1000]); for(Id accId: aMap.keyset()){ }

 

 

 There's more on governor limits here.

 

 

Cheers,

Wes 

JmBessonartJmBessonart

You can use list of lists to store the 10.000 records in a "list".

The code going to look like this:

 

List<List<Account>> accountList = new List<List<Account>>(); List<Account> tempList = new List<Account>(); for (Account iter : [select id from Account]) { tempList.add(iter); if (tempList.size() == 1000) { accountList.add(tempList); tempList.clear(); } } if (tempList.size() > 0) { accountList.add(tempList); tempList.clear(); }

 

 Regards,

J.

 

vabvab

In Spring 10 release, Limits on the number of items a collection can hold have been removed.

so there will be no worry for you.

ToddKruseToddKruse

2 Questions.

 

1.  When will Spring 10 release be released?

2.  If I create a List<List<Account>> object. How do I loop through all the records in the list of lists?

 

Thanks

 

--Todd Kruse