I need to create a List of accounts. Query all account records and store them in the list. Write a for loop on that list and pass values one by one into a map.

  • 6
    Great! So you don't want to try yourself. If we give you the solution for this how will you solve your other tasks without knowing concepts. Anyways welcome to Stackexchange please read this salesforce.stackexchange.com/help/how-to-ask when you want your next tasks to be done by us.
    – Ashwani
    Commented Jul 9, 2015 at 7:51

1 Answer 1


Have you reviewed any documentation in order to do that? There are thousand of places with code examples.

Also, you should specify what you really need. What kind of values do you need in the map? In any case these are my 2 options:

1- Get a list and pass to a map

 List<Account> accounts = new List<Account>();
 accounts = [Select Id, Name From Account]; //Add all fields you need

 Map<Id, Account> accountsMap = new Map<Id, Account>();
 for(Account acc : accounts)
      accountsMap.put(acc.Id, acc);

2- Above option in a single step

 Map<Id, Account> accountsMap = new Map<Id, Account> ([Select Id, Name From Account]); //Add all your fields in the query

Find here more information about maps

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .